这听起来可能很愚蠢。但是我对 D 编程语言有一个奇怪的问题。当我尝试创建这样的新数组时:
import std.stdio;
void main()
{
char[] variable = "value";
writefln(variable);
}
DMD 编译器总是给我这个错误:
test.d(5): Error: cannot implicitly convert expression ("value") of type invariant(char[5u]) to char[]
知道为什么吗?我正在为 Ubuntu 使用 2.014 alpha(可用 here)。
最佳答案
我正在搜索指南的数组部分,这可能会有所帮助:
A string is an array of characters. String literals are just an easy way to write character arrays. String literals are immutable (read only).
char[] str1 = "abc"; // error, "abc" is not mutable
char[] str2 = "abc".dup; // ok, make mutable copy
invariant(char)[] str3 = "abc"; // ok
invariant(char)[] str4 = str1; // error, str4 is not mutable
invariant(char)[] str5 = str1.idup; // ok, make invariant copy
来自 here .
关于arrays - D 编程语言字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/557011/