Merge pull request #37 from schveiguy/addNullHandling

Allow typeof(null) to provide constructors and opAssign for TaggedUnion.
This commit is contained in:
Sönke Ludwig 2019-11-01 09:00:47 +01:00 committed by GitHub
commit 3d0305da16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -75,15 +75,22 @@ align(commonAlignment!(UnionKindTypes!(UnionFieldEnum!U))) struct TaggedUnion
}
static foreach (ti; UniqueTypes!FieldTypes)
static if (!isUnitType!(FieldTypes[ti])) {
static if(!is(FieldTypes[ti] == void))
{
this(FieldTypes[ti] value)
{
set!(cast(Kind)ti)(move(value));
static if (isUnitType!(FieldTypes[ti]))
set!(cast(Kind)ti)();
else
set!(cast(Kind)ti)(move(value));
}
void opAssign(FieldTypes[ti] value)
{
set!(cast(Kind)ti)(move(value));
static if (isUnitType!(FieldTypes[ti]))
set!(cast(Kind)ti)();
else
set!(cast(Kind)ti)(move(value));
}
}
@ -496,6 +503,14 @@ unittest { // toString
assert(val.to!string == "(noCopy: foo)");
}
unittest { // null members should be assignable
union U { int i; typeof(null) Null; }
TaggedUnion!U val;
val = null;
assert(val.kind == val.Kind.Null);
val = TaggedUnion!U(null);
assert(val.kind == val.Kind.Null);
}
/** Dispatches the value contained on a `TaggedUnion` to a set of visitors.