www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Call a function passed as template parameter.

reply "moechofe" <truc moechofe.com> writes:
Hi,

I would like to create a template that take a function as 
template parameter, create an arguments list for it, fill it with 
some data and call the function.

void foo(uint a, string b)
{
   // ...
}

void bar(long a, long b, string c)
{
   // ...
}

call(alias F)(JSONValue j)
{
   // create arguments list
   // assign arguments with the value from j
   // call the function
}

void main()
{
   call!foo(parseJSON("[123, \"nice\"]"));
   call!bar(parseJson("[1,2,3]"));
}

I found interesting stuff like ParameterTypeTuple! and Tuple! but 
I'm not be able to make it work together.

How can I do that?
Sep 03 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 3 September 2015 at 11:31:22 UTC, moechofe wrote:
 I would like to create a template that take a function as 
 template parameter, create an arguments list for it, fill it 
 with some data and call the function.
Check out the sample chapter to my book: https://www.packtpub.com/application-development/d-cookbook This is one of the examples I explain there that uses this basic idea to convert strings to parameters: http://arsdnet.net/dcode/book/chapter_08/11/caller.d Or, a real world example I've written is here: https://github.com/adamdruppe/arsd/blob/master/jsvar.d#L608 which is similar to what you're trying to do, though JSONValue will be a bit harder to convert than my `var`.
Sep 03 2015
parent "moechofe" <truc moechofe.com> writes:
On Thursday, 3 September 2015 at 13:16:41 UTC, Adam D. Ruppe 
wrote:
 http://arsdnet.net/dcode/book/chapter_08/11/caller.d
 https://github.com/adamdruppe/arsd/blob/master/jsvar.d#L608
Thank you for these examples.
Sep 04 2015