November 25, 2015 convertion of type tuple to enum | ||||
---|---|---|---|---|
| ||||
What is the best way to do subj? I did ``` import std.array: array; import std.typetuple: TypeTuple; import std.typecons: tuple; import std.traits: EnumMembers; struct Foo {} struct Bar {} struct FooBar {} struct Baz {} string convertTypeTupleToEnum(Types...)() { string s = "enum Kind { "; foreach(T; Types) { s ~= T.stringof ~ ", "; } s ~= "}"; return s; } alias Types = TypeTuple!(Foo, Bar, FooBar, Baz); void main() { mixin(convertTypeTupleToEnum!(Types)); assert(EnumMembers!Kind.tuple.array == [ Kind.Foo, Kind.Bar, Kind.FooBar, Kind.Baz]); } ``` (also here http://dpaste.dzfl.pl/bc6f19770f85). Is there a way to do it without string mixin? Thanks! |
Copyright © 1999-2021 by the D Language Foundation