Nullable inout构造函数创建可变对象

时间:2014-11-14 17:29:14

标签: d

以下代码在尝试编译时给出了一个奇怪的错误:

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

1 个答案:

答案 0 :(得分:1)

默认情况下,关联数组文字是可变的。

要修复,您可以替换:

            NullableAAT naav = [KeyTInit:ValT.init];

使用:

            alias CAA = typeof(naa.get());
            CAA aa = [KeyTInit:ValT.init];
            NullableAAT naav = aa;

这将使用正确的const声明AA,它将通过构造函数的Nullable传播到inout

  

奇怪的事情(或者可能并不奇怪。可能是编译器在这么多错误之后就放弃了)是错误不存在于所有类型的组合中,只是那些将ubyte作为键类型的组合。 / p>

是的,它在第二次循环的第一次迭代后停止。您可以通过从元组头部删除类型来进行测试。