Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 847
Abstract type Avatar asked Nov 11 '14 09:11

Abstract type


1 Answers

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.

like image 138
Gassa Avatar answered Jan 01 '23 13:01

Gassa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!