Static Associative Arrays

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 7 21:01:40 PDT 2012


On Saturday, April 07, 2012 18:45:40 Caligo wrote:
> I'm not questioning the design, but I would like to know the reason:
> given the fact that associative arrays are built into the language,
> why don't we have static associative arrays?

What do you mean my static associative arrays? Are you asking why you can't 
initialize a static variable which is an AA at compile time? e.g.

static int[string] aa = ["hello" ; 7, "world", 13]; //error

That's because that would involve allocating memory at compile time which 
would then somehow have to be around at runtime. And that doesn't work right 
now. Classes have the same problem. You have to initialize them at runtime. I 
believe that dynamic arrays are the only ones that work with that right now, 
and that's because it's a difficult problem. Obviously, none of the memory 
allocated at compile time can persist to runtime, so there is no simple 
solution. At some point, it will probably be possible, but not right now. You 
_should_ be able to use them in CTFE, but they can't be assigned to anything 
that will persist until runtime.

The solution is to use static constructors to initialize AAs and class 
references at runtime. 

Now, if you're talking about something else, I have no idea what you mean by 
static AAs.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list