May 24, 2013 Re: immutable ctors, immutable members, and TDPL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 23 May 2013 at 15:41:32 UTC, Steven Schveighoffer wrote: > An interesting subject is being debated by some of the heavy hitters of D on the latest beta on the announce NG. > > However, I have found that there is an inconsistency in TDPL that needs to be addressed. > > Forgetting the controversy currently brewing, we have the following current situation: > > struct S > { > immutable int x; > this(int n) > { > x = n; > int y = x; > x = 0; > } > } > > This compiles, and shows that one can use an immutable, and then the immutable can be changed. This is no good, for obvious reasons. I think immutable is something you cannot change. Also, the reverse is true, if you can change something, it's not immutable. According to this logic, x is not immutable inside the scope of the constructor, because you *can* change it. Therefore it should be so that: import std.traits; struct S { immutable int x; this(int n) { static assert(isMutable!(typeof(x))); } } static assert(!isMutable!(typeof(S.init.x))); |
Copyright © 1999-2021 by the D Language Foundation