Static attributes & immutability, static attributes seen from instances
bearophile
bearophileHUGS at lycos.com
Fri Mar 5 13:33:40 PST 2010
This was a discussion I've started in the digitalmars.D.learn newsgroup, but div0 has suggested to move it here for a more general public.
This is a reduced version of a D2 program, written while I was trying to use/learn immutability. This program compiles:
struct Foo {
static int x;
}
void main() {
immutable Foo f;
Foo.x++;
f.x++;
}
My idea was that immutable applied to a struct makes every thing in such struct namespace immutable. I was wrong, but what do you think about changing a little how D works here?
A possible idea is: if one instance is 'const', then the static attribute x is seen as const from just that instance. If one instance is set to 'immutable' as here, then the static attributes become immutable in every instance of Foo.
A bit later in the discussion div0 and Pelle M. have said/suggested that accessing static vars through an instance can be a bad thing, and it's better to allow the programmer to access them only through the class/struct name.
Bye,
bearophile
More information about the Digitalmars-d
mailing list