What is the syntax for declaring a constant string[char] AA?

460 Views Asked by At

The following declaration:

const(string[char]) AA1 = [
    'a' : "fkclopel",
    'b' : "poehfftw"
];

void main(string args[]){}

gives me:

C:...\temp_0186F968.d(1,27): Error: non-constant expression ['a':"fkclopel", 'b':"poehfftw"]

while it would work with other type kinds.

1

There are 1 best solutions below

2
On BEST ANSWER

You can initialize associative array constants inside a module constructor:

const /+ or immutable +/ (string [char]) AA1;
static this () {
    AA1 = [
        'a' : "fkclopel",
        'b' : "poehfftw"
    ];
}

import std.stdio;
void main () {writeln (AA1);}

The manual section on associative array literals explicitly states that "An AssocArrayLiteral cannot be used to statically initialize anything.", though it does not give clues as to why it is so.