Is it possible to prevent reassignment of variables non-transitively in D2?
For example:
final int[] a = [0];
a[0] = 1; // OK.
a = []; // ERROR.
I only see const
and immutable
here:
http://www.dlang.org/const3.html
No. What you have is const
and immutable
, and they are transitive (they really wouldn't work if they weren't). You can do
const(int)[] a = [0];
a[0] = 1; // ERROR.
a = []; // OK;
But not what you're looking for.
The compiler can give better guarantees when const
and immutable
are transitive. Also, immutable
really isn't of any use for threading (one of its main reasons for existing) unless it's transitive, and because anything that's immutable
must be able to be const
, const
must be transitive as well. So, of necessity, they can't be used for simply protecting against variable reassignment. And there are no other constructs in the language for doing so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With