Changing behavior of associative array

Kevin Bailey keraba at yahoo.com
Sun Dec 17 00:10:56 UTC 2023


On Saturday, 16 December 2023 at 22:44:16 UTC, Dennis wrote:
>
> That's because `m[f] = 1` initializes the associative array to 
> something non-null. If you pass a `null` AA to a function which 
> adds things, the caller will still have a null pointers. You 
> can initialize a non-null empty AA like this:
>
> ```D
> uint[Foo] m = new uint[Foo];
> ```
>
> Then, `m` can be passed by value and you can make additions or 
> removals which the caller sees, unless you assign a new AA in 
> the function.

Thanks Dennis. I'll have to tweak my mental model of what's 
happening. I was picturing a std::map-like thing that was passed 
by reference, but instead it seems like 'm' is a pointer to a 
std::map, that is initialized on use if null. (I think it's that 
latter part that gives the illusion of being already initialized.)



More information about the Digitalmars-d-learn mailing list