声明常量字符串[char] AA的语法是什么?

时间:2014-11-11 09:36:32

标签: constants associative-array literals d

以下声明:

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

void main(string args[]){}

给了我:

  

C:... \ temp_0186F968.d(1,27):错误:非常量表达式[' a':" fkclopel",' b&#39 ;:" poehfftw"]

虽然它适用于其他类型。

1 个答案:

答案 0 :(得分:7)

您可以在模块构造函数中初始化关联数组常量:

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

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

关联数组文字上的manual section明确指出“An AssocArrayLiteral 不能用于静态初始化任何东西。”,尽管它没有提供关于它为何如此的线索。 / p>