Odd Associative Array Reference Behavior

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


On Thursday, 11 February 2016 at 03:47:09 UTC, Steven 
Schveighoffer wrote:
> Misunderstanding.
>
> An AA under the hood is simply a pointer. Initialized to null.
>
> When you pass it around, you are passing a pointer. AA assign 
> checks for null and allocates a new AA impl to hold the data. 
> But this doesn't affect other copies (that were null).
>
> So what is happening is aa() returns a null AA. You assign to 
> it, which allocates a new AA impl, and sets the rvalue to point 
> at it. The rvalue promptly disappears. The original m_aa is 
> still set to point at null.

Makes sense (though it defies my intuition; I would have expected 
an NPE or crash at time of assignment). Thanks!



More information about the Digitalmars-d-learn mailing list