D doesn't have real closures

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Sep 11 15:50:03 PDT 2007


"0ffh" <spam at frankhirsch.net> wrote in message 
news:fc6mea$4d8$1 at digitalmars.com...
> Brad Anderson wrote:
> I am sure with all the templating wizzards floating around here
> someone can come up with something that is not quite as full of
> crap as my little proof of concept code, probably with multiple
> parameter feature and whatnot; dirty and obvious hack for multi
> parm would be to have N copies of the template for 0..N-1 parms.

Oh please, D has variadic templates :D  That, and I'd expect something like 
this to be a value type.

struct MCD(Args...)
{
    void function(Args)[] funcs;

    void opCatAssign(void function(Args) f)
    {
        funcs ~= f;
    }

    void opCall(Args args)
    {
        foreach(f; funcs)
            f(args);
    }
}

void fa(int x)
{
    Stdout.formatln("fa({})", x);
}

void fb(int x)
{
    Stdout.formatln("fb({})", x);
}

void main()
{
    MCD!(int) a;
    a ~= &fa;
    a ~= &fb;
    a(3);
} 





More information about the Digitalmars-d-announce mailing list