www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static attributes aren' immutable

reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 05 2010
next sibling parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

Yes it's correct. x is really just a global variable, or a thread local variable if you are using a newer compiler. putting it in Foo simply puts it in a namespace. You could argue that 'f.x++' should not be allowed in order to emphasis that x is not part of an instance, but that could make writing generic code more awkward. IIRC, you can do: immutable struct Foo { static int x; } Then you can't write to x, but then you can't ever write to any instance members either. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLkRj0T9LetA9XoXwRAs+kAJ9ClMvkpPtODmsjcE1RudT+5rucEACgsPma yaNYtgO64gs+vYeEA0MfDTc= =wq60 -----END PGP SIGNATURE-----
Mar 05 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
div0:
putting it in Foo simply puts it in a namespace.<

So my (wrong) idea of immutable applied to a struct was that every thing in such namespace becomes immutable (I think this is a bit more intuitive). What do you think of modifying D2 so in a situation like the one I've shown even static arguments become const/immutable? Can this cause troubles to other things? Thank you to you and Lars T. Kyllingstad for the answers. Bye, bearophile
Mar 05 2010
parent reply =?ISO-8859-1?Q?Pelle_M=E5nsson?= <pelle.mansson gmail.com> writes:
On 03/05/2010 07:50 PM, bearophile wrote:
 div0:
 putting it in Foo simply puts it in a namespace.<

So my (wrong) idea of immutable applied to a struct was that every thing in such namespace becomes immutable (I think this is a bit more intuitive). What do you think of modifying D2 so in a situation like the one I've shown even static arguments become const/immutable? Can this cause troubles to other things? Thank you to you and Lars T. Kyllingstad for the answers. Bye, bearophile

Immutability (somewhat) guarantees the value will never ever change. The static attribute could be changed by a non-immutable instance, and can therefore not be immutable. It could be const for the immutable instance, but I don't see the gains from it. I do, however, see the gains from not being able to access the static members through an instance.
Mar 05 2010
parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pelle Månsson wrote:
 On 03/05/2010 07:50 PM, bearophile wrote:
 div0:
 putting it in Foo simply puts it in a namespace.<

So my (wrong) idea of immutable applied to a struct was that every thing in such namespace becomes immutable (I think this is a bit more intuitive). What do you think of modifying D2 so in a situation like the one I've shown even static arguments become const/immutable? Can this cause troubles to other things? Thank you to you and Lars T. Kyllingstad for the answers. Bye, bearophile

Immutability (somewhat) guarantees the value will never ever change. The static attribute could be changed by a non-immutable instance, and can therefore not be immutable. It could be const for the immutable instance, but I don't see the gains from it. I do, however, see the gains from not being able to access the static members through an instance.

Yeah, I *never* access static vars through an instance, I always use the class name. Somebody want to post in the main group? Might be worth a bigger discussion; perhaps somebody can provide a good use case for access through an instance we're not seeing. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLkWUuT9LetA9XoXwRAjTIAJ9kwfo1mKj4J7ghW+SoEYqMBDu/pACfaJyf YAfV9JMHUAUfsFWw3e5pTSg= =/Sfv -----END PGP SIGNATURE-----
Mar 05 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
div0:
 Somebody want to post in the main group?

OK, I'll do it soon :-) Bye, bearophile
Mar 05 2010
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
div0:
 Yeah, I *never* access static vars through an instance,
 I always use the class name.
 
 Somebody want to post in the main group?

It seems there are few people that agree with you, but Walter has not answered about this yet, so you can ask him his opinion. I like to use a tidy language. Bye, bearophile
Mar 06 2010
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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. -Lars
Mar 05 2010