Empty associative array
deadalnix
deadalnix at gmail.com
Mon Aug 9 12:16:44 UTC 2021
In D associative array have reference semantic. Well they do,
except when they are initially created, in which case they are
null. For instance:
```d
int[int] a1;
int[int] a2 = a1; // a1 is null, so a2 is null too.
a2[3] = 3; // a2 is initialized to something else than null here.
writeln(a1.length); // prints 0.
a1 = a2; // a1 and a2 now point to the same reference.
a2[4] = 4;
writeln(a1.length); // prints 2. a1 was modified with a2 now that
they point to the same thing.
```
There is an immediate problem with this: how does one gets an
empty, but non null, associative array?
I find myself in a situation where I use an AA to cache some
computation results, and this cache might be necessary in a
couple of places in the application. It works great, unless the
cache is initially empty, in which case, every location ends up
with its own cache, defeating the whole point of caching these
results to begin with.
More information about the Digitalmars-d
mailing list