Full closures for D

Witold Baryluk baryluk at smp.if.uj.edu.pl
Tue Nov 6 17:26:55 PST 2007


Dnia Fri, 02 Nov 2007 14:03:59 -0700
Walter Bright <newshound1 at digitalmars.com> napisał/a:

> D 2.007 brings full closures to D. I was tired of D being denigrated
> for not having "real" closures.
> 

Simpler curring?

// not tested
C delegate(B) curry(A, C, B...)(C delegate(A, B) dg_, A a_) {
        auto a = a_;
        auto dg = dg_;
        C add(B b) {
                return dg(a, b);
        }
        return &add;
}


instand of:

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

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;
}



Also other functional stuff like infinite lazy lists or monads can be
done much much simpler now :)

-- 
Witold Baryluk, aleph0



More information about the Digitalmars-d mailing list