I'm writing a system to register functions to be called at
runtime. With zero-argument functions, it works fine. However,
I run into a problem with functions that take arguments.
This is the relevant code I started with (zero-argument version):
mixin template CommandSystemRegister(string s = __MODULE__)
{
void CommandSystemRegisterCommands()
{
foreach(name; __traits(allMembers, mixin(s)))
{
static if (hasUDA!(mixin(name), RegisterCmd))
{
commandTable[name] = &mixin(name);
}
}
}
}
void CommandSystemExecuteCommand(string cmd)
{
auto result = cmd in commandTable;
if (result !is null)
{
(*result)();
}
else
{
writefln("command %s not found.", cmd);
}
}
The way to extend this seemed fairly straightforward. I did the
following things:
1. Wrap function with a Variant, and put that Variant into a
struct alongside an array of stringified parameter types (because
Parameters!T can't be stored directly).
2. On execution, parse the arguments to their correct types.
The problem is, I can't figure out how to actually *call* the
function. If it were python, I could construct a tuple with a
comprehension and unpack that, but I can't figure out any way to
dynamically construct tuples this way in D.
Basically, I need some way to turn an array of strings into an
argument list at runtime. Is this possible?