Mixin replacement for switch...case?

Jerome jerome.spamable at yahoo.com
Tue Oct 23 03:38:29 PDT 2012


OK. I have done my homework and answered my own question based on 
the Duff's Device example in the Language Reference page for 
Mixins.

The solution (not variadic though) would be:

mixin template Select!(alias value,
   alias if0, alias then0,
   alias if1, alias then1,
   alias if2, alias then2,
   alias thenDefault)
{
   switch(value) {
     case if0 : { then0(); } break;
     case if1 : { then1(); } break;
     case if2 : { then2(); } break;
     default : thenDefault();
   }
}

and it is used this way:

mixin Select!(value,
   if0, delegate { then0(); },
   if1, delegate { then1(); },
   if2, delegate { foo(); bar(); },
   delegate { thenDefault(); }
);

no gain at all verbosity-wise I'm afraid... nevermind.


More information about the Digitalmars-d mailing list