Improving std.typecons.defineEnum

Tomek Sowiński just at ask.me
Wed Oct 6 12:18:39 PDT 2010


I remember using defineEnum a few times. From user perspective you have enum names as 
strings that forms a nasty mixin, not to mention you can't Ddoc single enum values. It felt like 
stone age. Then I took a look at how it's implemented and saw a bunch of unreadable 
templates glueing together the content of an unholy string mixin.

But instead of complaining I took time to devise something nicer:

enum Eh { Ah, Oh, Uh }	// plain vanilla enum
mixin EnumUtils!Eh;  // that's all you need, magic happens here

assert (enumToString(Eh.Ah) == "Ah");
assert (enumToString(cast(Eh)666) == null);

Eh eh;
assert (enumFromString("Oh", eh));
assert (eh == Eh.Oh);
assert (!enumFromString("Heh", eh));


Do you want the EnumUtils template in Phobos?
If so, how to contribute?

-- 
Tomek


More information about the Digitalmars-d mailing list