Skip to content

Commit

Permalink
Add the TypeOf template.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Jan 17, 2018
1 parent de7df6b commit f87b378
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/taggedalgebraic.d
Expand Up @@ -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)
Expand Down

0 comments on commit f87b378

Please sign in to comment.