www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mutable reference for immutable AA

reply "Jack Applegame" <japplegame gmail.com> writes:
I need mutable storage for immutable associative array. Just 
create new immutable AA and store it for future passing it 
between threads/fibers.

First attempt: just immutable AA
immutable aa = ["1":1, "2":1];
aa = ["1":1, "2":1]; // fail, can't assign a new AA

Second attempt: mutable AA with immutable elements.
immutable (int)[string] aa = ["1":1, "2":1];
aa = ["1":1, "2":1]; // sucess!
aa["2"] = 2;         // doesn't compile, is AA immutable?
aa.remove("1");      // fail, this compiles, AA actually isn't 
immutable

Third attempt: Rebindable
Rebindable!(immutable int[string]) aa; // fail, Rebindable 
doesn't accept AAs
May 29 2015
parent "Jack Applegame" <japplegame gmail.com> writes:
I made trivial pull request - 
https://github.com/D-Programming-Language/phobos/pull/3341

RebindableAA!(immutable int[string]) aa = ["a": 1, "b": 2]; // 
works
assert(aa["a"] == 1);  // cool
aa = ["a": 3, "b": 4]; // nice
auto bb = aa;          // yes
bb = ["a": 4, "b": 5]; // super

aa["a"] = 2;           // error, good
aa.remove("a");        // error, very good
May 29 2015