2014-11-14 1 views
2

변경 가능한 객체를 생성합니다 :Null 허용 입출력의 생성자 컴파일 할 때 다음 코드는 나에게 이상한 오류를 제공

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

연관 배열 리터럴은 기본적으로 변경할 수 있습니다.

는 대체 할 수있는, 해결하려면 :

  NullableAAT naav = [KeyTInit:ValT.init]; 

로 :

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

이 생성자의 inout를 통해 Nullable에 전파 할 올바른 const와,와 AA를 선언합니다.

이상한 점은 컴파일러가 많은 오류를 포기한 것일 수도 있습니다. 오류가 모든 유형의 조합에 대해 존재하지 않는다는 것입니다. 단지 ubyte를 키로 사용하는 것뿐입니다. 유형.

예, 두 번째 루프의 첫 번째 반복 후에 중지됩니다. 튜플의 머리에서 유형을 제거하여 테스트 할 수 있습니다.

+0

그렇다면 AA는 아직 고유성 추론의 이점을 얻지 못했을 것입니다. 또는 리터럴이 const, 불변 등으로 유추 될 수없는 다른 것 때문입니까? – Meta

+0

AAs는 특별하지 않습니다. 코드는 constness가 변수 선언에서 Nullable inout 생성자를 통해 AA까지 전달되는 것을 기대합니다. 실제로 전파는 다른 방향으로 진행됩니다. 생성자의 매개 변수의 constness는 생성 된 객체의 constness를 정의합니다. –

관련 문제