digitalmars.D.learn - Static attributes aren' immutable
- bearophile (12/12) Mar 05 2010 I'm playing some more with immutables in D2. This program compiles:
- div0 (26/41) Mar 05 2010 -----BEGIN PGP SIGNED MESSAGE-----
- bearophile (6/7) Mar 05 2010 So my (wrong) idea of immutable applied to a struct was that every thing...
- =?ISO-8859-1?Q?Pelle_M=E5nsson?= (7/14) Mar 05 2010 Immutability (somewhat) guarantees the value will never ever change. The...
- div0 (18/42) Mar 05 2010 -----BEGIN PGP SIGNED MESSAGE-----
- bearophile (4/5) Mar 05 2010 OK, I'll do it soon :-)
- bearophile (4/8) Mar 06 2010 It seems there are few people that agree with you, but Walter has not an...
- Lars T. Kyllingstad (6/21) Mar 05 2010 It is correct, because x isn't a part of the struct, it's a global
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
-----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, bearophileYes 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
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
On 03/05/2010 07:50 PM, bearophile wrote:div0: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.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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Pelle Månsson wrote:On 03/05/2010 07:50 PM, bearophile wrote: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-----div0: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.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
div0:Somebody want to post in the main group?OK, I'll do it soon :-) Bye, bearophile
Mar 05 2010
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
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, bearophileIt 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