Problem with closures: Problem solved

Daniel Giddings dgiddings at bigworldtech.com
Thu Jan 11 14:34:50 PST 2007


There is also a templated Curry function example in the D template docs:

http://www.digitalmars.com/d/template.html

/* R is return type
  * A is first argument type
  * U is TypeTuple of rest of argument types
  */
R delegate(U) Curry(R, A, U...)(R delegate(A, U) dg, A arg)
{
     struct Foo
     {
	typeof(dg) dg_m;
	typeof(arg) arg_m;

	R bar(U u)
	{
	    return dg_m(arg_m, u);
	}
     }

     Foo* f = new Foo;
     f.dg_m = dg;
     f.arg_m = arg;
     return &f.bar;
}

void main()
{
     int plus(int x, int y, int z)
     {
	return x + y + z;
     }

     auto plus_two = Curry(&plus, 2);
     printf("%d\n", plus_two(6, 8));	// prints 16
}


More information about the Digitalmars-d-learn mailing list