Static attributes aren' immutable

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Fri Mar 5 07:24:10 PST 2010

bearophile wrote:
> I'm playing some more with immutables in D2. This program compiles:
> struct Foo {
>     static int x;
> }
> void main() {
>     immutable Foo f;
>     Foo.x++;
>     f.x++;
> }
> Is this code supposed to be correct? If I have an immutable struct I want all of it to be immutable...
> Bye and thank you,
> bearophile

It is correct, because x isn't a part of the struct, it's a global 
variable.  However, in my opinion that last line should cause a compiler 
error -- it shouldn't be possible to access static members through an 
instance of the struct.


More information about the Digitalmars-d-learn mailing list