Add the TypeOf template.

This commit is contained in:
Sönke Ludwig 2018-01-17 23:53:57 +01:00
parent de7df6bfeb
commit f87b378cf3

View file

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