Generating an enum from a tuple of Types?

Johannes Pfau nospam at example.com
Sat Jan 25 07:38:05 PST 2014


Is it possible to generate a enum from a tuple of types without string
mixins?
------------
struct S(Types...)
{
    enum Tag
    {
        //?
    }
}
------------

where the tag enum should have Types.length members. The exact names of
the enum members don't matter and could be numbered, for example:
Tag._1, Tag._2, ...

This is what I have right now, using string mixins:
http://dpaste.dzfl.pl/536e0be9

It implements a simple C-like tagged union in a generic way, so stuff
like this is possible:

-------------------------------------------
alias Value = TaggedUnion!(int, string);
auto val = Value("Hello");
auto vals = val.get!string();
val.set(0);
final switch(val.tag)
{
    case Value.tagType!int:
        break;
    case Value.tagType!string:
        break;
}
-------------------------------------------

It's basically std.variant Algebraic with less features, but it should
be much faster as it doesn't use TypeInfo.


More information about the Digitalmars-d-learn mailing list