Empty associative array

Dukc ajieskola at gmail.com
Mon Aug 9 13:10:53 UTC 2021


On Monday, 9 August 2021 at 12:16:44 UTC, deadalnix wrote:
> 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.

It turns out the dark corners with empty static arrays don't stop 
with initialization. One can inefficiently hack an empty static 
array:

```d
import std;

@safe:

int[int] emptyAA;

void setEmptyAA()
{ alias Key = int, Value = int;

   emptyAA = [Key.init: Value.init];
   auto remover = emptyAA;
   remover.remove(Key.init);
}

void main()
{ setEmptyAA;
   int[int] a = emptyAA;
   int[int] b = a;

   b[5] = 3;

   a.writeln; //[5:3]
}
```

...however, if you initialize `a` with `emptyAA.dup` it will 
again be `null`!

I believe we can only conclude that empty AA's are not supposed 
to rely on being non-`null`. Sorry Walter - in my eyes this is a 
design failure.


More information about the Digitalmars-d mailing list