www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - You know, it's just occurred to me...

reply "Janice Caron" <caron800 googlemail.com> writes:
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. :-)
Dec 02 2007
next sibling parent Derek Parnell <derek psych.ward> writes:
On Sun, 2 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...
 ... my expectation that "const(X)" should mean the same thing as "const X" ...
"Walter says that's not going to change. You just have to deal with it." - Janice Caron -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 02 2007
prev sibling next sibling parent Leandro Lucarella <llucax gmail.com> writes:
Please, use more descriptive subjects.

Thank you.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Do not get mad with others
Because they know more than you
It is not their fault
Dec 02 2007
prev sibling next sibling parent mandel <oh no.es> writes:
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 }
Dec 02 2007
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
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.
Dec 02 2007
parent Sean Kelly <sean f4.ca> writes:
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
Dec 03 2007