From f0d324f43fa16801175dd1ec23f44a4029a11155 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Wed, 30 Oct 2019 18:12:50 -0400 Subject: [PATCH] Allow typeof(null) to provide constructors and opAssign for TaggedUnion. Fixes #36. --- source/taggedalgebraic/taggedunion.d | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/source/taggedalgebraic/taggedunion.d b/source/taggedalgebraic/taggedunion.d index 7fe07e7..939988c 100644 --- a/source/taggedalgebraic/taggedunion.d +++ b/source/taggedalgebraic/taggedunion.d @@ -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.