www.digitalmars.com         C & C++   DMDScript  

D - a~=b VS a=a~b

reply "Keir" <keir verizon.net> writes:
given char[] a;

the line
    a~=args[0][j];
compiles (and works) just fine. But
    a=a~args[0][j];
does not and gives the message
incompatible types for ((a) ~ (cast(int)(args[0][j]))): 'char[]' and 'int'

are these two lines not functionally identical?

Along a similar vein, I've noticed that given...
char x;
char[] a;
char[] b;

trying to do something like

b=b~x;

or

b=b~a[i];

gives the same issue with the non-explicit cast to int.  I haven't been able
to get around it.
Apr 07 2003
next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Keir" <keir verizon.net> escribiσ en el mensaje
news:b6ssmk$k7r$1 digitaldaemon.com...
| given char[] a;
|
| the line
|     a~=args[0][j];
| compiles (and works) just fine. But
|     a=a~args[0][j];
| does not and gives the message
| incompatible types for ((a) ~ (cast(int)(args[0][j]))): 'char[]' and 'int'
|
| are these two lines not functionally identical?
|
| Along a similar vein, I've noticed that given...
| char x;
| char[] a;
| char[] b;
|
| trying to do something like
|
| b=b~x;
|
| or
|
| b=b~a[i];
|
| gives the same issue with the non-explicit cast to int.  I haven't been
able
| to get around it.
|
|

It's because int and char are basically the same. It's "one of those", like
trying this:

void foo(char x) { ... }
void foo(char[] x) {...}
...
foo('a');

Just doesn't compile. You have to explicitly cast.

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 2003-04-01
Apr 07 2003
parent reply "Keir" <keir verizon.net> writes:
although, that doesn't answer why ~= works and =x~ doesn't... its entirely
possible that I'm casting incorrectly...

I've tried, so far, ...
b=b~x
b=b~(char)x;
b=b~(char[])x;
b=b~cast(char)x;
and
b=b~cast(char[])x;

so far, none of these have worked.  the error from the compiler insists that
it should be cast as an (int)... any idea's on how to override this? or what
I'm doing incorrectly?

"Carlos Santander B." <carlos8294 msn.com> wrote in message

It's because int and char are basically the same. It's "one of those", like
trying this:

void foo(char x) { ... }
void foo(char[] x) {...}
...
foo('a');

Just doesn't compile. You have to explicitly cast.

"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:b6sstb$kfa$1 digitaldaemon.com...

"Keir" <keir verizon.net> escribiσ en el mensaje
news:b6ssmk$k7r$1 digitaldaemon.com...
| given char[] a;
|
| the line
|     a~=args[0][j];
| compiles (and works) just fine. But
|     a=a~args[0][j];
| does not and gives the message
| incompatible types for ((a) ~ (cast(int)(args[0][j]))): 'char[]' and 'int'
|
| are these two lines not functionally identical?
|
| Along a similar vein, I've noticed that given...
| char x;
| char[] a;
| char[] b;
|
| trying to do something like
|
| b=b~x;
|
| or
|
| b=b~a[i];
|
| gives the same issue with the non-explicit cast to int.  I haven't been
able
| to get around it.
|
|

It's because int and char are basically the same. It's "one of those", like
trying this:

void foo(char x) { ... }
void foo(char[] x) {...}
...
foo('a');

Just doesn't compile. You have to explicitly cast.

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 2003-04-01
Apr 08 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Keir" <keir verizon.net> escribiσ en el mensaje
news:b6v61u$29m6$1 digitaldaemon.com...
| although, that doesn't answer why ~= works and =x~ doesn't... its entirely
| possible that I'm casting incorrectly...
|
| I've tried, so far, ...
| b=b~x
| b=b~(char)x;
| b=b~(char[])x;
| b=b~cast(char)x;
| and
| b=b~cast(char[])x;
|
| so far, none of these have worked.  the error from the compiler insists
that
| it should be cast as an (int)... any idea's on how to override this? or
what
| I'm doing incorrectly?

I know it doesn't answer it. I was just pointing a case.

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 2003-04-01
Apr 08 2003
parent Ilya Minkov <midiclub 8ung.at> writes:
Carlos Santander B. wrote:
 | so far, none of these have worked.  the error from the compiler insists
 that
 | it should be cast as an (int)... any idea's on how to override this? or
 what
 | I'm doing incorrectly?
 
 I know it doesn't answer it. I was just pointing a case.
IIRC it was a guard against some common-made mistake.
Apr 09 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Keir" <keir verizon.net> wrote in message
news:b6ssmk$k7r$1 digitaldaemon.com...
 given char[] a;

 the line
     a~=args[0][j];
 compiles (and works) just fine. But
     a=a~args[0][j];
 does not and gives the message
 incompatible types for ((a) ~ (cast(int)(args[0][j]))): 'char[]' and 'int'

 are these two lines not functionally identical?

 Along a similar vein, I've noticed that given...
 char x;
 char[] a;
 char[] b;

 trying to do something like

 b=b~x;

 or

 b=b~a[i];

 gives the same issue with the non-explicit cast to int.  I haven't been
able
 to get around it.
I'm a bit nervous about making (char[] ~ char) work, as it could be a common source of bugs.
May 23 2003
next sibling parent reply "Keir" <keir verizon.net> writes:
is there an appropriate way to append characters to the end of a string?

"Walter" <walter digitalmars.com> wrote in message
news:bampoh$10mn$1 digitaldaemon.com...


I'm a bit nervous about making (char[] ~ char) work, as it could be a common
source of bugs.
May 27 2003
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
 is there an appropriate way to append characters to the end of a string?
I've been using sequences like: uint uibuf; char c; char[] str; . . . uibuf = str.length; str.length = str.length + 1; str[uibuf] = c; But shouldn't we be able to do something like: str ~ [c]; I think we should. - C. Sauls
May 30 2003
parent Ilya Minkov <midiclub 8ung.at> writes:
C. Sauls wrote:
is there an appropriate way to append characters to the end of a string?
--- 8< ---(unappropriate kludge)--- >8 ---
 But shouldn't we be able to do something like:
     str ~ [c];
 I think we should.
Not without assigment, although the general idea is right. Currently, i believe "str ~= c;" works. Surprise surprise. :/ The syntax is inconsistent. At least it was listed in Pavel's trick chest. -i.
May 30 2003
prev sibling parent Ilya Minkov <Ilya_member pathlink.com> writes:
In article <bampoh$10mn$1 digitaldaemon.com>, Walter says...
I'm a bit nervous about making (char[] ~ char) work, as it could be a common
source of bugs.
I believe this notation is also inconsistent - as it suggests 2 things being at the same level of abstraction when they aren't. And thus is a perfect source of confusion. When a single character (/value) is to be appended to an array, it has to be inclosed into an array literal syntax! -i.
May 28 2003