digitalmars.D - Improving std.typecons.defineEnum
- Tomek =?UTF-8?B?U293acWEc2tp?= (17/17) Oct 06 2010 I remember using defineEnum a few times. From user perspective you have ...
- bearophile (4/5) Oct 06 2010 Isn't it deprecated now?
- Tomek =?UTF-8?B?U293acWEc2tp?= (5/8) Oct 06 2010 Ah.. it is, got an old release.
- Tomek =?UTF-8?B?U293acWEc2tp?= (4/6) Oct 06 2010 Actually, you don't even need the mixin, just 2 functions...
- Andrei Alexandrescu (6/21) Oct 06 2010 That's a good contribution, but I just deprecated enumToString in a
- Tomek =?UTF-8?B?U293acWEc2tp?= (16/21) Oct 06 2010 Thanks, I just saw std.conv -- looks much better.
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
Oct 06 2010
Tomek S.:I remember using defineEnum a few times.Isn't it deprecated now? Bye, bearophile
Oct 06 2010
bearophile napisał:Ah.. it is, got an old release. Still, do you want my stuff? -- TomekI remember using defineEnum a few times.Isn't it deprecated now?
Oct 06 2010
Tomek Sowiński napisał:enum Eh { Ah, Oh, Uh } // plain vanilla enum mixin EnumUtils!Eh; // that's all you need, magic happens hereActually, you don't even need the mixin, just 2 functions... -- Tomek
Oct 06 2010
On 10/6/10 14:18 CDT, Tomek Sowiński wrote: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?That's a good contribution, but I just deprecated enumToString in a recent commit because D's current introspection abilities made it easy to define parse and to!string to manipulate enum names. I'll be looking forward to other goodies! Andrei
Oct 06 2010
Andrei Alexandrescu napisał:That's a good contribution, but I just deprecated enumToString in a recent commit because D's current introspection abilities made it easy to define parse and to!string to manipulate enum names. I'll be looking forward to other goodies!Thanks, I just saw std.conv -- looks much better. BTW, instead of: foreach (i, e; EnumMembers!Target) { auto ident = __traits(allMembers, Target)[i]; if (s.skipOver(ident)) return e; } you can: foreach (ident; __traits(allMembers, Target)) if (s.skipOver(ident)) return __traits(getMember, Target, ident); But that's nitpicking :) -- Tomek
Oct 06 2010