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

Michal Minich michal.minich at gmail.com
Tue Jul 2 06:51:14 PDT 2013


On Tuesday, 2 July 2013 at 13:39:36 UTC, Dicebot wrote:
> On Tuesday, 2 July 2013 at 13:33:10 UTC, Michal Minich wrote:
>> Ok I understand. What I did as a first thing when I get error 
>> on "char[] x = "a" was "char x = cast(char[])"a", Which was 
>> obviously incorrect - as the "a" was/should be placed in rom. 
>> So if this expression is allays wrong - casting string literal 
>> to mutable, then compiler should emit an error on this
>
> explicit cast means a order from a programmer "type system, I 
> know what I am doing, don't even try to bother me". You should 
> never use casts to simply suppress errors (unless absolutely 
> sure) in the very first place.

I completely agree.

The other thing is, for the less aware programmers, a warning on 
non-linux and a error on linux might be nice. As this cast (on 
linux), will _allways_ result in undefined behavior. It is 
something compiler can  easily tell, and advice "use .dup instead 
of cast..."


More information about the Digitalmars-d-announce mailing list