Provide nicer aliases for fields with trailing underscore.

This commit is contained in:
Sönke Ludwig 2020-04-17 16:47:20 +02:00
parent d24256a907
commit d50f507368

View file

@ -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));