Why implicit conversion of string literal to char[] does not works?

Dicebot public at dicebot.lv
Tue Jul 2 04:51:03 PDT 2013


On Tuesday, 2 July 2013 at 11:33:16 UTC, Michal Minich wrote:
>> char[] str = "abc".dup;
>
> Thanks, that is workaround I didn't know about.
>
> I'm really interested about reasons why it doesn't works 
> (without dup/cast). At some point it had to be disabled. But I 
> really cannot find a reason why that would be useful.

It is not a workaround but an expected approach. You are trying 
to get a mutable slice from an immutable array. There is no way 
this can be legal. And allowing mutable string literals just asks 
for trouble:

char[] str1 = "abc";
char[] str2 = "abc";
str1[0] = 'x';
assert(str2 == "abc"); // pass of fail?

Please note that this works:

char[3] str = "abc";

Here literal data is copied to a static array and concerns that 
matter with a slice are not valid anymore.


More information about the Digitalmars-d-announce mailing list