Static Associative Arrays

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Apr 7 22:37:53 PDT 2012


On Sat, Apr 07, 2012 at 09:01:40PM -0700, Jonathan M Davis wrote:
> 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.
[...]

I've thought of a way to use CTFE to generate static AA's, but I appear
to be running into a CTFE bug or quirk that causes it not to work.
Basically, the idea is to use CTFE to generate a series of static struct
declarations (each struct being an AA slot) with precomputed references
between them. Obviously, the resulting AA must be immutable, but in
principle this should allow implementation of static AA's.

But I'm running into a weird issue where using a string mixin with a
string constant containing the struct definitions work correctly, but if
the string is generated piecewise by CTFE code then all references turn
into null. I'll have to massage the code a bit into a minimal test case
and open an issue on the bugtracker.


T

-- 
Windows 95 was a joke, and Windows 98 was the punchline.


More information about the Digitalmars-d-learn mailing list