Associative Arrays in the data segment

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 10 00:54:47 PDT 2015


One long-standing issue with AAs has been that you can't generate constant 
ones at compile time and access them at run-time.  Workarounds are 
available, like initializing them in static this or using horribly 
inefficient enum AAs.

I've opened https://github.com/D-Programming-Language/dmd/pull/4571 which 
allows code like this:

immutable int[int] aa = [1 : 7, 3 : 2];

void main()
{
    assert(aa[1] == 7);
    assert(aa[3] == 2);
}

It only works with integral types of at most 32-bits at the moment, but most 
built-in types are fairly easy to support.  The downside is requires 
re-implementing druntime's AA and hashing algorithms in the compiler, and 
keeping them in sync when either changes.

I think it's worthwhile, even if only integral and string keys are supported 
for now.  That would cover 99% of my AA use.

Is anybody else interested in seeing this in the next release? 



More information about the Digitalmars-d mailing list