AA initialization

Puming via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 6 04:32:39 PDT 2014


I found AA initialization have a strange effect:

```d
	string[string] map; // same as `string[string] map = 
string[string].init;
	writeln(map); // output: []

	string[string] refer = map; // make a reference
	refer["1"] = "2"; // update the reference
	writeln(map); // output: []. The reference does not affect the 
original.
```

But if I do an assignment first, the reference works:


```d
	string[string] map; // same as `string[string] map = 
string[string].init;
	writeln(map); // output: []

         map["1"] = "0";

	string[string] refer = map; // make a reference
	refer["1"] = "2"; // update the reference
	writeln(map); // output: ["1":"2"]. The reference does affect.
```

So if I want an empty AA and want to use a variable to refer to 
it later, I have to do this:

```d
	string[string] map; // same as `string[string] map = 
string[string].init;

         map["1"] = "0";
         map.remove("1"); // assign and then REMOVE!
	writeln(map); // output: []

	string[string] refer = map; // make a reference
	refer["1"] = "2"; // update the reference
	writeln(map); // output: ["1":"2"]. The reference does affect.
```

which looks awkward.

Is it because `string[string].init == null` ? If so, how do I 
specify an empty AA which is not null? Neither `[]` or `[:]` 
seems to work.




More information about the Digitalmars-d-learn mailing list