Pergunta

Is there a way to create an immutable associative array in D? There doesn't seem to be a way to define an associative array; only declare one.

immutable char[][char[]] = ["testk" = "testv", "testk2" = "testv2"];
Foi útil?

Solução 2

You should use ":" instead of "=".

immutable (char[][char[]]) = ["testk": "testv", "testk2": "testv2"];

Outras dicas

Well you can define the values of an immutable associative array within a constructor.

Ex.

static immutable int[string] myArray;

static this()
{
    myArray["hi"] = 100;
}

You may want to use a mutable buffer first and assigning that to the immutable buffer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top