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

Michal Minich michal.minich at gmail.com
Tue Jul 2 04:26:58 PDT 2013


On Tuesday, 2 July 2013 at 10:09:10 UTC, John Colvin wrote:
> The reason you can't implicitly convert a string to a char[] is 
> because string is an alias to immutable(char)[]
>
> Therefore, you would be providing a mutable window to immutable 
> data, which is disallowed unless you *explicitly* ask for it 
> (see below).
would be -> might be
>
> You can explicitly cast string to char[], but it is undefined 
> behaviour to modify the contents of the result

ok

  (i.e. don't cast
> away immutable unless you absolutely have to and even then be 
> very very careful).

This is not correct. You should can not cast when you 'absolutely 
have to' but when you can prove by means not available to 
compiler that the data being casted are in fact mutable. And the 
'proving' of this is what you should be careful about.

But ... I'm asking only about implicit conversion of string 
literal, not arbitrary expressions of string type.


More information about the Digitalmars-d-announce mailing list