const in dmd v2.011

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 21 06:05:15 PST 2008


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

Besides that, a string (which is an invariant(char)[]) should be assingable 
to a const, because you can implicitly cast the mutable array to a const 
array, and implictly cast an invariant char to a const char (for the element 
type).  So I would expect both i and j to compile.

>
> The person who can document all this 'const' implementation so that most
> people can grasp it and remember it, will win lots of brownie points.
>

I think the tough part to grasp right now is the 'why' not the 'how'.  Once 
people stop debating the why part, the how becomes as simple as a set of 
examples and explanation of how to use it.

-Steve 




More information about the Digitalmars-d-learn mailing list