February 21, 2008 Re: const in dmd v2.011 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | "Derek Parnell" wrote
> On Thu, 21 Feb 2008 09:05:15 -0500, Steven Schveighoffer wrote:
>> Well, the lines I was thinking of were (from your original post):
>> const(char[]) i = "abc";
>> const(char[]) j = "xyz"; // ?? FAILS "Error: j[0] is not mutable"
>>
>> Why does i work, but j does not? Both lines seem identical in syntax.
>
> They are identical but the compiler is a bit too smart. The first one compiles without complaint because the 'manifest' constant it declares is never actually used so it optimizes it out of the picture. The second ones 'fails' to compile because I attempt to use 'j' as an lvalue later on. The error message is very misleading, but accurate too.
Ah... no this is not the case :)
You are attempting to use j as an lvalue later on. that is what causes the error. If you used j as an rvalue, then the compiler would not complain.
I think the problem here is that you are interpreting the compiler error as saying "line X failed to compile, j[0] is not mutable", when it really is saying "j[0] is not mutable because it was declared that way on line X". I guess it's really just a confusing error message.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation