From d50f507368f171aaa9d5c51826530f6694adfb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 17 Apr 2020 16:47:20 +0200 Subject: [PATCH] Provide nicer aliases for fields with trailing underscore. --- source/taggedalgebraic/taggedunion.d | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/source/taggedalgebraic/taggedunion.d b/source/taggedalgebraic/taggedunion.d index 5e8591d..9ce35fa 100644 --- a/source/taggedalgebraic/taggedunion.d +++ b/source/taggedalgebraic/taggedunion.d @@ -207,9 +207,18 @@ align(commonAlignment!(UnionKindTypes!(UnionFieldEnum!U))) struct TaggedUnion mixin("alias set"~pascalCase(name)~" = set!(Kind."~name~");"); mixin("@property bool is"~pascalCase(name)~"() const { return m_kind == Kind."~name~"; }"); + static if (name[$-1] == '_') { + mixin("alias set"~pascalCase(name[0 .. $-1])~" = set!(Kind."~name~");"); + mixin("@property bool is"~pascalCase(name[0 .. $-1])~"() const { return m_kind == Kind."~name~"; }"); + } + static if (!isUnitType!(FieldTypes[i])) { mixin("alias "~name~"Value = value!(Kind."~name~");"); + // remove trailing underscore from names like "int_" + static if (name[$-1] == '_') + mixin("alias "~name[0 .. $-1]~"Value = value!(Kind."~name~");"); + mixin("static TaggedUnion "~name~"(FieldTypes["~i.stringof~"] value)" ~ "{ TaggedUnion tu; tu.set!(Kind."~name~")(.move(value)); return tu; }"); @@ -523,6 +532,22 @@ unittest { // make sure field names don't conflict with function names val.setMove(1); } +unittest { // support trailing underscores properly + union U { + int int_; + } + TaggedUnion!U val; + + val = TaggedUnion!U.int_(10); + assert(val.int_Value == 10); + assert(val.intValue == 10); + assert(val.isInt_); + assert(val.isInt); + val.setInt_(20); + val.setInt(20); + assert(val.intValue == 20); +} + enum isUnitType(T) = is(T == Void) || is(T == void) || is(T == typeof(null));