Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prevent reassignment of variables non-transitively in D2?

Tags:

d

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

like image 491
XP1 Avatar asked Feb 16 '12 17:02

XP1


1 Answers

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.

like image 122
Jonathan M Davis Avatar answered Nov 10 '22 16:11

Jonathan M Davis