Difference betwee storage class and type (invariant/const)?

Daniel919 Daniel919 at web.de
Mon Jun 18 13:10:52 PDT 2007


That's exactly what I was wondering about.
Initially I already expected
"invariant(int) x" to be the same as "invariant int x".

But this confused me:
invariant(int) x = 1; x = 2;
writefln(x);
It's compiling and prints out 2.
Did I get something wrong, or is it (what I guess now) a misbehaviour?

Shouldn't it be the same as:
invariant int x = 1; x = 2; //Error: constant x is not an lvalue


The brackets are only needed, if you want:
sth-mutable pointing to sth-invariant
For example if you want a (mutable) ptr to an invariant int:
invariant(int)* x
You need them, because invariant/const is transitive.
Without brackets it applies to everything right to it.
If you use brackets, the const/invariant only applies to what is in 
brackets.

It's a good design, because naturally the other way round can't be possible:
sth-invariant can't point to sth-mutable


More information about the Digitalmars-d-learn mailing list