const in dmd v2.011

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 21 07:31:08 PST 2008


"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 




More information about the Digitalmars-d-learn mailing list