Thread overview
generating switch case from compile time sequence of functions
Jul 14, 2019
Sjoerd Nijboer
Jul 14, 2019
Adam D. Ruppe
Jul 15, 2019
Sjoerd Nijboer
July 14, 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`

July 14, 2019
On Sunday, 14 July 2019 at 19:26:41 UTC, Sjoerd Nijboer wrote:
> The code it must generate for `doSwitch!(foo, bar)()` is
> `{
>     switch (int)
>     {
>         foo:
>             foo();
>             return;
>         bar:
>             bar();
>             return;
>     }
> }`
>

I'd probably just do

void doSwitch(items...)(int i) {
   switch(i) {
      static foreach(idx, item; items) {
         case idx:
             item();
             return;
      }
   }
}


That should work pretty simply.

>     enum temp = [FunctionNames].join(", ");
>
>     enum switchEnum = "{" ~ temp ~ "};";

Were you trying to do a mixin here? The error you mention below is trying to use this switchEnum thing as a type... and it isn't a type, it is just a string. the mixin() is necessary to compile it into code and thus create that type.

mixin("enum switchEnum = { " ~ temp ~ "}");

but I think even attempting this is overcomplicating.

>         static foreach (name; FunctionNames)
>         {
>             name ~ " : " ~ name ~ "(); break;";
>         }

ditto down here too.
July 15, 2019
On Sunday, 14 July 2019 at 19:59:36 UTC, Adam D. Ruppe wrote:
> but I think even attempting this is overcomplicating.
>
>>         static foreach (name; FunctionNames)
>>         {
>>             name ~ " : " ~ name ~ "(); break;";
>>         }

I eventually went with

`switch (mixin(index))
{
    static foreach (index, name; FunctionNames)
    {
        mixin(index) ~ " : " ~ mixin(name) ~ "(); break;";
    }
    default:
        throw new Exception("Out of range.");
}`

I also ditched the idea of an enum for now, but I might add something simular in later for logging purposes.
Thank you for your help!