Is there something like "apply"?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 28 10:54:09 PDT 2015


On 04/28/2015 10:48 AM, Robert M. Münch wrote:
> Hi, I have the following "problem": I have the parameters for a function
> in an array. Now I want to call a function with a specific arity and use
> the parameters from the array to call it.
>
> Like this pseudo-code:
>
> args = [10, 20];
>
> def foo(a, b): return a + b;
>
> print(foo(*args));
>
> Is something like this possible in D?
>

import std.stdio;
import std.typetuple;

void foo(int i, string s, double d)
{
     writefln("foo: %s %s %s", i, s, d);
}

void main()
{
     auto args = TypeTuple!(42, "hello", 1.5);
     ++args[0];
     args[1] ~= " world";
     args[2] *= 3;
     foo(args);
}

Ali



More information about the Digitalmars-d-learn mailing list