Iterating over an enum associative array

Peter Lundgren lundgrpb at rose-hulman.edu
Mon Mar 14 07:38:49 PDT 2011


== Quote from Nebster (Evil.Nebster at gmail.com)'s article
> Hey,
> I'm having some problems iterating over an enumerated associative array.
> It comes up with this error at compile time:
>  > Internal error: e2ir.c 4835
> I cut the code down to this:
>  > import std.stdio;
>  >
>  > enum int[string] assoc = [";": 0, "=": 1, "+": 2, "-": 2, "*": 3,
> "/": 3];
>  >
>  > void main()
>  > {
>  > 	foreach(op; assoc.byKey())
>  > 		writefln("op: %s", op);
>  > }
> What does this mean/how can I get this to work?
> Thanks,
> Nebster


That's a rather cryptic error message. Hopefully someone smarter than I can trace
down why this doesn't have better error reporting. I suspect the problem has
something to do with CTFE. Because you're using an enum, D assumes you want its
value to be computed at compile time. However, currently, associative arrays can
not be evaluated at compile time. Using a static this to initialize the value at
runtime, as already suggested, should solve your issue.


More information about the Digitalmars-d-learn mailing list