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
그렇다면 AA는 아직 고유성 추론의 이점을 얻지 못했을 것입니다. 또는 리터럴이 const, 불변 등으로 유추 될 수없는 다른 것 때문입니까? – Meta
AAs는 특별하지 않습니다. 코드는 constness가 변수 선언에서 Nullable inout 생성자를 통해 AA까지 전달되는 것을 기대합니다. 실제로 전파는 다른 방향으로 진행됩니다. 생성자의 매개 변수의 constness는 생성 된 객체의 constness를 정의합니다. –