You know, it's just occurred to me...

Sean Kelly sean at f4.ca
Mon Dec 3 07:55:04 PST 2007


Walter Bright wrote:
> 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.

I think what Janice is saying is that if const(int)[] is an array of 
integers which cannot be modified, then why can the value "const(int) x" 
be modified?  There is an apparent lack of consistency with the meaning 
of "const(int)" between array and standalone declarations.


Sean



More information about the Digitalmars-d mailing list