Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 05, 2010 Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
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 |
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments: | 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 |
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
|
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to div0 | 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
|
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle Månsson Attachments: | 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 |
March 05, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to div0 | div0:
> Somebody want to post in the main group?
OK, I'll do it soon :-)
Bye,
bearophile
|
March 06, 2010 Re: Static attributes aren' immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to div0 | 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
|
Copyright © 1999-2021 by the D Language Foundation