以下代码在试图编译时给出了一个奇怪的错误:
import std.conv: to;
import std.typecons;
import std.traits;
void main()
{
alias BuiltinScalars = TypeTuple!(ubyte, byte, ushort, short, uint, int, ulong, long,
float, double, real, char, wchar, dchar, bool,
ifloat, idouble, ireal, cfloat, cdouble, creal);
foreach (T; BuiltinScalars)
{
foreach (ValT; BuiltinScalars)
{
alias KeyT = T;
alias AAT = ValT[KeyT];
foreach (NullableAAT; TypeTuple!(Nullable!AAT, const(Nullable!AAT), immutable(Nullable!AAT)))
{
NullableAAT naa;
assert(naa.to!string() == "Nullable.null");
static if (!isSomeString!KeyT)
enum KeyTInit = KeyT.init;
else
enum KeyTInit = `""`;
NullableAAT naav = [KeyTInit:ValT.init];
assert(naav.to!string() == '[' ~ KeyTInit.to!string() ~ ':' ~ ValT.init.to!string() ~ ']');
}
}
}
}我不知道这个代码有什么问题。Nullable只有一个构造函数,带有签名的this(inout T value) inout。
奇怪的事情(也许并不奇怪)。可能是编译器在出现了这么多错误之后就放弃了),因为错误并不存在于所有类型的组合中,只有那些将ubyte作为一个关键类型的类型。完整的错误输出是:
bug.d(46): Error: inout constructor std.typecons.Nullable!(ubyte[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(byte[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(ushort[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(short[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(uint[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(int[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(ulong[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(long[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(float[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(double[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(real[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(char[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(wchar[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(dchar[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(bool[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(ifloat[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(idouble[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(ireal[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(cfloat[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(cdouble[ubyte]).Nullable.this creates mutable object, not immutable
bug.d(46): Error: inout constructor std.typecons.Nullable!(creal[ubyte]).Nullable.this creates mutable object, not immutable发布于 2014-11-14 17:42:44
默认情况下,关联数组文字是可变的。
要修复,您可以替换:
NullableAAT naav = [KeyTInit:ValT.init];通过以下方式:
alias CAA = typeof(naa.get());
CAA aa = [KeyTInit:ValT.init];
NullableAAT naav = aa;这将以正确的常数声明AA,这将通过构造函数的Nullable传播到inout。
奇怪的事情(也许并不奇怪)。可能是编译器在出现了这么多错误之后就放弃了),因为错误并不存在于所有类型的组合中,只有那些将ubyte作为一个关键类型的类型。
是的,它在第二个循环的第一个迭代之后停止。您可以通过从元组的头部移除类型来进行测试。
https://stackoverflow.com/questions/26935600
复制相似问题