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

懵懂的女人 提交于 2019-12-14 03:46:05

问题


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:


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.



来源:https://stackoverflow.com/questions/26861708/what-is-the-syntax-for-declaring-a-constant-stringchar-aa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!