struct vs. class, int vs. char.

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 28 10:13:27 PDT 2009


On Tue, 28 Apr 2009 13:00:36 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

>> MLT:
>> 2. char[] vs. int[]
>> I think it is strange that
>> char[] x = "1234" ;
>> x[0] = '4' ;
>>
>> Produces a run time error, but
>> int[] x = [1,2,3,4] ;
>> x[0] = 4 ;
>> Doesn't. I think that they both should, or both shouldn't - to be  
>> consistent (and it would be better if they both didn't).
>
> I leave this to other people. in D2 strings are immutable, by the way,  
> while static arrays are not.

This has been discussed.  Most people agree that the behavior should be  
consistent.  Any array literal should be exactly that -- an unchangable  
literal.  In D2, it should be an immutable literal (and produce a compile  
time error if you try and change it).

BTW, this behavior you noticed is only on certain OSes.  On other OSes (I  
think windows), you get weird behavior:

char[] x = "1234";
x[0] = '4'; // no runtime error
char[] y = "1234"; // y actually now equals "4234"!

-Steve



More information about the Digitalmars-d mailing list