www.digitalmars.com         C & C++   DMDScript  

D - a question...

reply "Giancarlo Bellido" <vicentico1 hotmail.com> writes:
hello,
im just starting to program in D, and i have a question, maybe you can help
me. how can i do something like this?:

    char c = '2';
    char[] a= "34324";
    a = c + a;    // result : 234324

vicentico
May 10 2003
next sibling parent reply Jonathan Andrew <Jonathan_member pathlink.com> writes:
In D, combining characters is accomplished with the ~ operator, for
concatenation. So your code would be

char c = '2';
char[] a = "34324";
a = c ~ a;

-Jon

In article <b9kfbi$16he$1 digitaldaemon.com>, Giancarlo Bellido says...
hello,
im just starting to program in D, and i have a question, maybe you can help
me. how can i do something like this?:

    char c = '2';
    char[] a= "34324";
    a = c + a;    // result : 234324

vicentico

May 11 2003
next sibling parent "Giancarlo Bellido" <vicentico1 hotmail.com> writes:
I have tried this but i get an error saying:

C:\DMD\SAMPLES\BASE.D(15): incompatible types for ((cast(int)(a)) ~ (str)):
'int' and 'char[]'

it appears that a char is always casted as an integer and that
there is not a concatenation operator for chars and char arrays.

Should I just make a function to do this?

"Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
news:b9lab4$1uko$1 digitaldaemon.com...
 In D, combining characters is accomplished with the ~ operator, for
 concatenation. So your code would be

 char c = '2';
 char[] a = "34324";
 a = c ~ a;

 -Jon

 In article <b9kfbi$16he$1 digitaldaemon.com>, Giancarlo Bellido says...
hello,
im just starting to program in D, and i have a question, maybe you can


me. how can i do something like this?:

    char c = '2';
    char[] a= "34324";
    a = c + a;    // result : 234324

vicentico


May 11 2003
prev sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
Walter, i can see signs of a great omission!!!

Jonathan Andrew wrote:
 a = c ~ a;

This most obviously cannot work. I would expect something like "a = [c] ~ a" to work, but it won't either! IIRC it is possible to write "c ~= a" (isn't it in the Pavel's "stack trick" example?), but hey, it doesn't make anything better! it's just an exception form the rule: it simply complicates the language! Why is it better that "c ~= [a]"? In fact, it's worse! There has to be a possibility to define arrays on-the-go. Why is "int[] ar = [1,2,3]" valid as a global, yet invalid as a local? Because you cannot define arrays in-line. For what reason, actually? This is why you stick to var-args functions: because you cannot compose an array at the time of a function call. If you could, you wouldn't need them at all. These are kind of things i'm sick of in D. -i.
May 12 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
I have complained about the very selfsame thing, recently.  I hope our words
do not fall upon deaf ears.

I realize that D tries to move all "static" work into one place, such that
the compiler doesn't allow you to declare stuff that works at the static, or
module, scope, in a function.  Also it won't allow direct initialization of
array local variables, either, because that would end up generating code at
the start of the function?

So what if the compiler generates code for something, does it at init time,
so long as the semantics work out right.  But I can't stand having to hop
back to module scope to do things that should be possible in local scope.
And I really hate having to declare temporary named variables to specify
something that should be possible to specify as a literal.

You should be able to construct an array at any time, anywhere an array
expression is possible.  For instance, this should work:

char[] foo = { 'a', 'b', 'c' };

so should this:

char a,b,c;
char[] garbage = { a, b, c };

And so should this:

void foo(char[]);

int main()
{
    foo( { 'd', argv[1][0], 'e' } );
    return 0;
}

And ideally if I did this it'd work:

template foo(int i)
{
    struct bar { const int value = i }
}

void work()
{
    const int[] which = { 1, 2 };
    const int first = instance foo(which[0]).bar.value;
    const int second = instance foo(which[1]).bar.value;
}

I just don't want D to be a PITA to use.  Plain and simple.  The less
special cases, the better.

Sean

"Ilya Minkov" <midiclub 8ung.at> wrote in message
news:b9p1hn$2pa3$1 digitaldaemon.com...
 Walter, i can see signs of a great omission!!!

 Jonathan Andrew wrote:
 a = c ~ a;

This most obviously cannot work. I would expect something like "a = [c] ~ a" to work, but it won't either! IIRC it is possible to write "c ~= a" (isn't it in the Pavel's "stack trick" example?), but hey, it doesn't make anything better! it's just an exception form the rule: it simply complicates the language! Why is it better that "c ~= [a]"? In fact, it's worse! There has to be a possibility to define arrays on-the-go. Why is "int[] ar = [1,2,3]" valid as a global, yet invalid as a local? Because you cannot define arrays in-line. For what reason, actually? This is why you stick to var-args functions: because you cannot compose an array at the time of a function call. If you could, you wouldn't need them at all. These are kind of things i'm sick of in D.

May 12 2003
prev sibling parent Manfred Hansen <manfred toppoint.de> writes:
Hello,

the easiest way is to change  char c = '2'  to 
char[] c = '2' and then a = c ~ a;
You can only concatenate array's .

my other way is

import string;
int main() {
        char c = '2';
        char[] a = "34324",d;

        char[] toStr(char c) { 
                char [] res;
                res ~= c;
                return res;
        }
        a = a ~ toStr(c);
        printf("%.*s\n",a);
        return 0;
}

Best regards 
Manfred

Giancarlo Bellido wrote:

 hello,
 im just starting to program in D, and i have a question, maybe you can
 help me. how can i do something like this?:
 
     char c = '2';
     char[] a= "34324";
     a = c + a;    // result : 234324
 
 vicentico

May 11 2003