声明常量字符串[char] AA的语法是什么? [英] What is the syntax for declaring a constant string[char] AA?

查看:119
本文介绍了声明常量字符串[char] AA的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下声明:

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

void main(string args[]){}

给我:

C:... \ temp_0186F968.d(1,27):错误:非恒定表达式['a':"fkclopel",'b':"poehfftw"]

C:...\temp_0186F968.d(1,27): Error: non-constant expression ['a':"fkclopel", 'b':"poehfftw"]

它可以与其他类型的文本一起使用.

while it would work with other type kinds.

推荐答案

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

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);}

关于关联数组文字的手册部分明确指出," AssocArrayLiteral 不能用于静态地初始化任何东西.",尽管它没有提供线索说明为什么这样做.

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.

这篇关于声明常量字符串[char] AA的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆