digitalmars.D.learn - generating switch case from compile time sequence of functions
- Sjoerd Nijboer (41/41) Jul 14 2019 I am trying to create a template function with a switch case
- Adam D. Ruppe (19/37) Jul 14 2019 I'd probably just do
- Sjoerd Nijboer (14/19) Jul 15 2019 I eventually went with
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`
Jul 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.
Jul 14 2019
On Sunday, 14 July 2019 at 19:59:36 UTC, Adam D. Ruppe wrote:but I think even attempting this is overcomplicating.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!static foreach (name; FunctionNames) { name ~ " : " ~ name ~ "(); break;"; }
Jul 15 2019








Sjoerd Nijboer <dlang sjoerdnijboer.com>