generating switch case from compile time sequence of functions

Sjoerd Nijboer dlang at sjoerdnijboer.com
Sun Jul 14 19:26:41 UTC 2019


I am trying to create a template function with a switch case 
inside it.
The function signature is:
`static void doSwitch(T...)(int i)`

The code it must generate for `doSwitch!(foo, bar)()` is
`{
     switch (int)
     {
         foo:
             foo();
             return;
         bar:
             bar();
             return;
     }
}`

It would be nice if this function would cast `i` to an enum too 
so that I can put down a breakpoint in a debugger and maybe add 
some logging, but that is not strictly neccesary.


The code I have right now is:
`
template switchEnum(FunctionNames...)
{
     enum temp = [FunctionNames].join(", ");

     enum switchEnum = "{" ~ temp ~ "};";
}

static void doSwitch(FunctionNames...)(int i)
{
     auto functionName = cast(switchEnum!FunctionNames) i;

     switch (functionName)
     {
         static foreach (name; FunctionNames)
         {
             name ~ " : " ~ name ~ "(); break;";
         }
     }
}
`
But I can't get it to work and am hitting a dead end.
The error I get:
`Error: switchEnum!(foo, bar) is used as a type`



More information about the Digitalmars-d-learn mailing list