Static attributes & immutability, static attributes seen from instances

Nick Sabalausky a at a.a
Fri Mar 5 21:50:32 PST 2010


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:hmrtbk$1aoi$1 at digitalmars.com...
> 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

I've always felt that the ability to access static members through an 
instance was just a bad idea in general, and this seems to add another 
reason not to allow it.





More information about the Digitalmars-d mailing list