www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array references

reply "Atila Neves" <atila.neves gmail.com> writes:
Bug or feature? This one caught me by surprise:

void main() {
     {
         int[string] aa[string];
         aa["foo"]["bar"] = 1;
         assert(aa["foo"]["bar"] == 1); //ok

         auto aa2 = aa;
         aa2["boo"]["dar"] = 2;
         assert(aa["foo"]["bar"] == 1); //ok
     }

     {
         int[string] aa[string]; //not set to anything yet

         auto aa2 = aa;
         aa2["boo"]["dar"] = 2;
         assert(aa["foo"]["bar"] == 1); //oops
     }
}

It seems that assigning an AA to another makes both point at the 
same data only if the first array has data to begin with. Is that 
the expected behaviour?

Atila
Dec 02 2013
next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote:
 Bug or feature? This one caught me by surprise:

 void main() {
     {
         int[string] aa[string];
         aa["foo"]["bar"] = 1;
         assert(aa["foo"]["bar"] == 1); //ok

         auto aa2 = aa;
         aa2["boo"]["dar"] = 2;
         assert(aa["foo"]["bar"] == 1); //ok
     }

     {
         int[string] aa[string]; //not set to anything yet

         auto aa2 = aa;
         aa2["boo"]["dar"] = 2;
         assert(aa["foo"]["bar"] == 1); //oops
     }
 }

 It seems that assigning an AA to another makes both point at 
 the same data only if the first array has data to begin with. 
 Is that the expected behaviour?

 Atila
I would say it is consequence of current implementation, and, taking into account ongoing efforts to rewrite AA implementation, this special detail of null AA behavior may one day be gone.
Dec 02 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Atila Neves:

         int[string] aa[string];
Don't mix C style array definitions with D style ones :-) Always use the D-style, unless you are porting C code (and refactor it later). Bye, bearophile
Dec 02 2013
parent reply "Atila Neves" <atila.neves gmail.com> writes:
On Monday, 2 December 2013 at 15:13:48 UTC, bearophile wrote:
 Atila Neves:

        int[string] aa[string];
Don't mix C style array definitions with D style ones :-) Always use the D-style, unless you are porting C code (and refactor it later).
How would that go in this case?
Dec 02 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Atila Neves:

 How would that go in this case?
An example usage: void main() { int[float][string] aa; aa["foo"][1.5] = 1; } Bye, bearophile
Dec 02 2013
parent "Atila Neves" <atila.neves gmail.com> writes:
On Monday, 2 December 2013 at 16:52:51 UTC, bearophile wrote:
 Atila Neves:

 How would that go in this case?
An example usage: void main() { int[float][string] aa; aa["foo"][1.5] = 1; } Bye, bearophile
Oh. That makes sense. Doh! I guess I was focussing on "key to AA of..." and that's why I wrote it that way.
Dec 03 2013
prev sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote:
 It seems that assigning an AA to another makes both point at 
 the same data only if the first array has data to begin with. 
 Is that the expected behaviour?

 Atila
You've been hit by the null associative array not being the same as an empty associative array. See discussion: http://forum.dlang.org/post/wovnuggqtscsbgnjcpua forum.dlang.org
Dec 02 2013
parent "Atila Neves" <atila.neves gmail.com> writes:
On Tuesday, 3 December 2013 at 03:14:46 UTC, Jesse Phillips wrote:
 On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote:
 It seems that assigning an AA to another makes both point at 
 the same data only if the first array has data to begin with. 
 Is that the expected behaviour?

 Atila
You've been hit by the null associative array not being the same as an empty associative array. See discussion: http://forum.dlang.org/post/wovnuggqtscsbgnjcpua forum.dlang.org
Sigh. If at least there was a way to declare them empty...
Dec 03 2013