Cannot initialize associative array.

bearophile bearophileHUGS at lycos.com
Tue Jun 22 15:08:10 PDT 2010


Bernard Helyer:
> AAs can't be assigned to at compile time (:[).

You can define enum ones, this works:

import std.stdio;
enum int[string] aa = ["foo": 10];
void main() {
    writeln(cast(bool)("foo" in aa));
    writeln(aa["foo"]);
    writeln(cast(bool)("hello" in aa));
}


But this code:

import std.stdio;
immutable int[string] aa = ["foo": 10];
void main() {
    writeln(cast(bool)("foo" in aa));
    writeln(aa["foo"]);
    writeln(cast(bool)("hello" in aa));
}

Raises the compilation error:
test.d(2): Error: non-constant expression ["foo":10]
In theory aa here is a constant expression :-)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list