Thread overview
Call a function passed as template parameter.
Sep 03, 2015
moechofe
Sep 03, 2015
Adam D. Ruppe
Sep 04, 2015
moechofe
September 03, 2015
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?
September 03, 2015
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`.
September 04, 2015
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.