diff --git a/source/taggedalgebraic.d b/source/taggedalgebraic.d index 37d7792..200c7fe 100644 --- a/source/taggedalgebraic.d +++ b/source/taggedalgebraic.d @@ -658,6 +658,34 @@ unittest { } +/** Maps a kind enumeration value to the corresponding field type. + + `kind` must be a value of the `TaggedAlgebraic!T.Kind` enumeration. +*/ +template TypeOf(alias kind) + if (isInstanceOf!(TypeEnum, typeof(kind))) +{ + import std.traits : FieldTypeTuple, TemplateArgsOf; + alias U = TemplateArgsOf!(typeof(kind)); + alias TypeOf = FieldTypeTuple!U[kind]; +} + +/// +unittest { + static struct S { + int a; + string b; + string c; + } + alias TA = TaggedAlgebraic!S; + + static assert(is(TypeOf!(TA.Kind.a) == int)); + static assert(is(TypeOf!(TA.Kind.b) == string)); + static assert(is(TypeOf!(TA.Kind.c) == string)); +} + + + /** Gets the value stored in an algebraic type based on its data type. */ ref inout(T) get(T, U)(ref inout(TaggedAlgebraic!U) ta)