Thread overview |
---|
July 11, 2016 How can you call a stored function in an AA with proper number of arguments converted to the proper type? | ||||
---|---|---|---|---|
| ||||
Here's the basic code I'm playing with: struct MyCmd { Variant func; // Has other members. } MyCmd[string] functions_; void addCommand(T)(const string name, T func) { MyCmd cmd; cmd.func = Variant(func); functions_[name] = cmd; } void process(string[] args) // args is only available at runtime. { const string name = args[0]; // Name of the command if(name in functions_) { MyCmd cmd = functions_[name]; cmd.func(/*Call with proper number of arguments converted to the proper type*/); } } I initially got idea from the D Cookbook Chapter Reflection: Creating a command-line function caller. But the code has to reside in the same module as the functions it will use. So I arrived at this code but can't figure out how to call the actual stored function. Thanks! |
July 12, 2016 Re: How can you call a stored function in an AA with proper number of arguments converted to the proper type? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zekereth | Store a wrapper instead of the actual function: void wrapper(alias F)(string[] args) { (convert args to F arguments) and invoke } cmd.func = &wrapper!someFunc; string[] args; cmd.func(args); |
July 12, 2016 Re: How can you call a stored function in an AA with proper number of arguments converted to the proper type? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Tuesday, 12 July 2016 at 08:34:03 UTC, Kagamin wrote:
> Store a wrapper instead of the actual function:
> void wrapper(alias F)(string[] args)
> {
> (convert args to F arguments) and invoke
> }
>
> cmd.func = &wrapper!someFunc;
>
> string[] args;
> cmd.func(args);
Thanks that is clever. Never would have thought of that. Thanks a lot!
|
Copyright © 1999-2021 by the D Language Foundation