Abs*_*ype 5 associative-array d constants literals
以下声明:
const(string[char]) AA1 = [
'a' : "fkclopel",
'b' : "poehfftw"
];
void main(string args[]){}
Run Code Online (Sandbox Code Playgroud)
给我:
C:...\temp_0186F968.d(1,27):错误:非常量表达式['a':"fkclopel",'b':"poehfftw"]
而它适用于其他类型.
您可以在模块构造函数中初始化关联数组常量:
const /+ or immutable +/ (string [char]) AA1;
static this () {
AA1 = [
'a' : "fkclopel",
'b' : "poehfftw"
];
}
import std.stdio;
void main () {writeln (AA1);}
Run Code Online (Sandbox Code Playgroud)
关联数组文字的手册部分明确指出"一个AssocArrayLite不能用于静态初始化任何东西.",尽管它没有提供关于它为什么如此的线索.