Const Ideas
Leandro Lucarella
llucax at gmail.com
Fri Nov 30 17:00:17 PST 2007
Adam D. Ruppe, el 30 de noviembre a las 13:38 me escribiste:
> int a;
> Adds an entry to the compiler's variable table named 'a' that references
> memory that is an int.
>
> const(int) a;
> Add an entry named 'a' that references memory that is a constant int. The
> const() CTF takes the type int and returns a new type that is a constant int.
>
> const int a;
> Add a *constant* entry named 'a' that references memory that is a mutable
> int.
But now you lost the idea of transitive constness (if your compile-time
pointer is const, the value it points to should be const). This doesn't
break your "model" if you think as if D works like Python, where int are
inmutables, but you can rebind the variable (your compile-time pointer).
Then:
const int a = 1;
means "I have a variable named 'a' that points to some int that has the
value 1 and I can't change where it points to (I can't rebind it)".
a = 2; is an error
const(int) b = 1;
means "I have a variable named 'b' that points to some int, conceptually
the same int as 'a', because there is no need to have more than 2
inmutable ints with the same value in the program, but I can change where
'b' points to (I can rebind it).".
so b = 2; is ok
So graphically:
+---+
a -> | 1 | <- b
+---+
and after b = 2;
+---+ +---+
a -> | 1 | b -> | 2 |
+---+ +---+
Of course this is a "conceptual" model, otherwise there would not be value
passing and must be a reference internall, which I think is not that
efficient ;)
This is basically what happens with pointers and the values they point to.
--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Did you know the originally a Danish guy invented the burglar-alarm
unfortunately, it got stolen
More information about the Digitalmars-d
mailing list