const in dmd v2.011

Derek Parnell derek at psych.ward
Thu Feb 21 06:30:47 PST 2008


On Thu, 21 Feb 2008 09:05:15 -0500, Steven Schveighoffer wrote:

> "Derek Parnell"  wrote
>> On Wed, 20 Feb 2008 22:39:03 -0500, Steven Schveighoffer wrote:
>>
>>> I think some of the issues you point out, especially where you have 2
>>> seemingly similar statements where one statement gives an error and the
>>> other does not, are real bugs that should be filed.
>>
>> After a bit more examination, it is probably not a bug but just a poor
>> error message. It turns out that
>>
>>  const(char[]) s = "abc";
>>
>> is essentially treated as a manifest constant. It is almost identical to
>>
>>  enum (s = "abc"}
> 
> 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.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list