> 文章列表 > d的初化问题

d的初化问题

d的初化问题

原文

immutable long[string] aa = ["foo": 5,"bar": 10,"baz": 2000];

可如下:

//错误,在编译时初化,全局变量
long[string] globalAa = ["foo": 5, "bar": 10];void main()
{// 无误long[string] localAa = ["baz": 2000, "quux": 9999];
}

还可用静态模块构造器

long[string] globalAa;static this()
{globalAa = ["foo": 5, "bar": 10];
}

这在运行时,程序启动时调用.
你可用复制运行时类型布局的库类型得到.我在newaa库中这样,这里:

immutable Hash!(long, string) aa = ["foo": 5,"bar": 10,"baz": 2000
];

用法有点粗糙,还有些未实现.但是索引工作,应该对你的用例工作.
但是它可用asAA转换为AA(不确定是否支持不变,我没有太多测试).
-史蒂夫