You know, it's just occurred to me...
Walter Bright
newshound1 at digitalmars.com
Sun Dec 2 22:57:48 PST 2007
Janice Caron wrote:
> There's a general expectation in D that for all T, T[] is an array of
> T. Well, it's not always true... See,
>
> const(int)[] a;
>
> does not declare a to be an array of "const(int)" at all.
Yes, it does.
> It declares
> a to be an array of "const int" (without the parentheses).
Why do you think it does?
> The
> difference is that (under the peculiar syntax we have right now with
> D2.008), "const(int)" is mutable (!), wheras "const int" is const - as
> demonstrated by the following code.
>
> const(int) x;
> const int y;
> x = 1; // OK
> y = 2; // Error
>
> Yet we write "const(int)[]", not "(const int)[]". Huh?
There is the type, and the storage class. Const with parens sets the
type, const without parens sets the storage class. That's why we don't
write (const int).
> So, not only is my expectation that "const(X)" should mean the same
> thing as "const X" confounded, but now it turns out that my
> expectation that "T[]" means "array of T" is also confounded.
Why? The expectation is that const(int)[] is an array of ints that
cannot be modified through the array reference, and it is.
More information about the Digitalmars-d
mailing list