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

Regan Heath regan at netmail.co.nz
Tue Jul 2 06:07:40 PDT 2013


On Tue, 02 Jul 2013 12:33:14 +0100, Michal Minich  
<michal.minich at gmail.com> wrote:

> On Tuesday, 2 July 2013 at 11:29:05 UTC, Dicebot wrote:
>> On Tuesday, 2 July 2013 at 11:26:59 UTC, Michal Minich wrote:
>>> But ... I'm asking only about implicit conversion of string literal,  
>>> not arbitrary expressions of string type.
>>
>> 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 done for performance reasons.  On UNIX the compiler will put the  
literal "abc" into read only memory.  It could/should do the same on  
windows but doesn't yet (I believe).

So, the compiler is treating them as such, by giving them the type  
immutable(char)[] (AKA string).

And, the spec should, if it doesn't, define string literals to be  
immutable.

In older versions of DMD we didn't have string AKA immutable(char), in  
fact at one point we didn't have immutable at all.  At that time, "abc"  
was typed as char[] and people on UNIX systems ran headlong into an access  
violation attempting to write to ROM :p

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-announce mailing list