Jump to page: 1 2
Thread overview
Why implicit conversion of string literal to char[] does not works?
Jul 02, 2013
Michal Minich
Jul 02, 2013
Michal Minich
Jul 02, 2013
John Colvin
Jul 02, 2013
Michal Minich
Jul 02, 2013
Dicebot
Jul 02, 2013
Michal Minich
Jul 02, 2013
Dicebot
Jul 02, 2013
Andrej Mitrovic
Jul 02, 2013
Regan Heath
Jul 02, 2013
Michal Minich
Jul 02, 2013
Dicebot
Jul 02, 2013
Michal Minich
Jul 02, 2013
Jesse Phillips
Jul 02, 2013
Michal Minich
Jul 02, 2013
Regan Heath
Jul 02, 2013
Regan Heath
Jul 02, 2013
John Colvin
Jul 02, 2013
Maxim Fomin
July 02, 2013
void main ()
{
    char[] str = "abc";
    // Error: cannot implicitly convert expression ("abc")
    // of type string to char[]
}

I thought there is no harm in that?
July 02, 2013
Sorry for posting to announce.
July 02, 2013
On Tuesday, 2 July 2013 at 09:11:44 UTC, Michal Minich wrote:
> Sorry for posting to announce.

In future please post these questions to digitalmars.D.learn

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).

You can explicitly cast string to char[], but it is undefined behaviour to modify the contents of the result (i.e. don't cast away immutable unless you absolutely have to and even then be very very careful).
July 02, 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.
July 02, 2013
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;
July 02, 2013
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.


July 02, 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.
July 02, 2013
On 7/2/13, Michal Minich <michal.minich@gmail.com> wrote:
> I'm really interested about reasons why it doesn't works (without
> dup/cast).

It's probably to avoid implicit memory allocations.
July 02, 2013
On Tue, 02 Jul 2013 12:33:14 +0100, Michal Minich <michal.minich@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/
July 02, 2013
On Tuesday, 2 July 2013 at 11:26:59 UTC, Michal Minich wrote:
> 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

no, "would be" is correct. char is mutable, immutable(char) is not. By definition char[] is a mutable view of data, immutable(char)[] is not.

>>
>> 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.

Eek... no, unless i'm misunderstanding you or you made a typo, that's not the case at all.

Don't mutate anything that has a live (and used?)* immutable reference to it.

Just because the data was mutable before being cast to immutable, doesn't mean you can just cast away immutable and mutate it.


Or, alternatively presented:

You can cast to or away from immutable when you can prove by any means that the data being casted are not mutated through any reference after the creation of or before the final use of any immutable references.

Erring on the safe side, you might want to wait for the immutable references to be actually dead and gone before any mutation.


* Would like some clarification on whether it is the usage of the immutable reference or the lifetime that counts, if anyone knows for sure.
« First   ‹ Prev
1 2