Initializing "two-dimensional" compile-time enum

Philippe Sigaud philippe.sigaud at gmail.com
Sun Nov 24 05:57:56 PST 2013


Geez, I just spent 15' trying to make this work! AA + Compile-time are
like oil and water.

You can use CTFE and an initializing function. It's a bit cumbersome,
but it works.

module main;

import std.stdio;

string[int][string] initializePohod()
{
    string[int][string] result;
    result["vid"] = [ 3: "skiing", 5: "rafting", 7: "jumping" ];
    result["ks"] = [ 1: "first", 2: "second", 3: "third" ];
    result["prepare"] = [ 1:"planning", 3:"preparing", 5:"complete" ];
    return result;
}

enum string[int][string] pohodEnumValues = initializePohod();

void main()
{
        writeln(pohodEnumValues);
        pragma(msg, pohodEnumValues); // there, accessible during compilation.
}

On Sun, Nov 24, 2013 at 1:01 PM, Uranuz <neuranuz at gmail.com> wrote:
> Greetings! I have a problem in my code and I need an advice. I need
> possibility of creating "two-dimensional" AA in module scope that I can
> access at compile-time. I considered that it should be marked as enum to do
> this but I get strange error. There is a piece of code to test.
>
> //---------------
> import std.stdio;
>
>
> enum string[int][string] pohodEnumValues = [
>         "vid": [ 3: "skiing", 5: "rafting", 7: "jumping" ],
>         "ks": [ 1: "first", 2: "second", 3: "third" ],
>         "prepare": [ 1:"planning", 3:"preparing", 5:"complete" ]
> ];
>
>
> void main()
> {
>         writeln(pohodEnumValues);
>
> }
> //--------END OF CODE -----
>
> In dmd 2.064.2 I get following output:
>
> Compilation output:
> /d521/f517.d(4): Error: not an associative array initializer
>
> Elements of AA are used as template arguments so I need them at
> compile-time. I need help, please))


More information about the Digitalmars-d mailing list