Odd Associative Array Reference Behavior

Matt Elkins via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 10 19:10:51 PST 2016


Consider the following definition of Foo and an accompanying 
unittest:

[code]
struct Foo
{
     @property int[int] aa() {return m_aa;}
     @property ref int[int] aaRef() {return m_aa;}
     int[int] m_aa;
}

unittest
{
     Foo foo;
     assert(5 !in foo.m_aa); // Sanity-check to start off
     foo.aa[5] = 1;          // Add an element with key 5
     assert(5 !in foo.m_aa); // ...huh. 5 didn't make it in?
     foo.aaRef[5] = 1;       // Try again, using the ref variant
     assert(5 in foo.m_aa);  // Works!
}
[/code]

I was under the impression that associative arrays are reference 
types; if I pass a non-ref "copy" of one, shouldn't insertions 
still be reflected in the original? Am I dealing with a bug or a 
misunderstanding on my part?


More information about the Digitalmars-d-learn mailing list