How do I defeat the gratuitous qualification of alias members?

Chad Joan chadjoan at gmail.com
Thu Apr 4 23:20:04 PDT 2013


On 04/04/2013 11:50 PM, Chad Joan wrote:
...

FWIW, I was able to make a template to allow me to do what I want:

-------------------
mixin template dequalifyEnumMembers(theEnum, members...)
{
     static if ( members.length > 0 )
     {
         mixin("alias "~theEnum.stringof~"."~members[0]
             ~" "~members[0]~";");
         mixin dequalifyEnumMembers!(theEnum, members[1..$]);
     }
}

mixin template dequalifyEnum(theEnum) if (is(theEnum == enum))
{
     mixin dequalifyEnumMembers!(theEnum, __traits(allMembers, theEnum));
}
-------------------

Usage:

-------------------
mixin dequalifyEnum!MyEnum;
enum MyEnum
{
     Pliers,
     Forceps,
     Picks,
}

void foo(MyEnum m)
{
     final switch(m)
     {
         // Comment one of these lines out to get the
         //   "not represented in enum" error.
         case Pliers:  writeln("Pliers");  break;
         case Forceps: writeln("Forceps"); break;
         case Picks:   writeln("Picks");   break;
     }
}

void main()
{
     foo(Picks);
}
-------------------

I still can't escape the feeling that this is a hack to work around 
limitations of the language or lack of knowledge.


More information about the Digitalmars-d-learn mailing list