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

mandel oh at no.es
Sun Dec 2 18:40:47 PST 2007


On Sun, 02 Dec 2007 21:10:25 +0000, 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. It declares a
> to be an array of "const int" (without the parentheses). 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?
> 
> 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.
> 
> You can just imagine how easy this is going to be to explain to newbies.
> :-)

I haven't tried the new const yet (using 2.008),
but I did some simple test and I found the results beeing reasonable.

const(int) is a restriction of the const to the int data of the array.
So we get less const protection;
Therefore array.ptr and array.length are mutable (allowing assignment).

void main()
{
	const(int) x;
	const int y;
	x = 1; // OK
	y = 2; // Error
	
	const(int)[] xx;
	const int[] yy;
	
	xx = null; //OK
	xx[0] = 1; //Error
	
	yy = null; //Error
	yy[0] = 1; //Error
}



More information about the Digitalmars-d mailing list