1.0 ?? [templates and currying]

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Nov 6 19:09:37 PST 2006


"Walter Bright" <newshound at digitalmars.com> wrote in message 
news:eiosfr$283p$1 at digitaldaemon.com...

> I'm writing an std.traits module, which will abstract away that stuff. So, 
> if you're willing to rewrite using std.traits.Returns and std.traits 
> .Parameters, you'll be immunized against changes in the future.
>
> Also, to get number of parameters for function foo:
>
> Parameters!(foo).length
>
> will do it. First parameter type is:
>
> Parameters!(foo)[0]
>
> Rest of the parameter types are:
>
> Parameters!(foo)[1 .. length]
>
> Etc.

Sounds great.

Just dreaming:

template bind(alias func, dchar[] name = nameof(func))()
{
    alias RetType!(func) ReturnType;
    alias Parameters!(func) Params;

    // Creates a MiniD-friendly function which acts as a shim between
    // MiniD and the real function.
    // GOD I wish I could create identifiers using token pasting.
    int identifier("bound_" ~ nameof(func))(MDState s)
    {
        // Create a new tuple big enough to hold all the parameter values
        alias NTuple!(Params.length) paramValues;

        // Get the params off the MiniD stack
        foreach(i, paramType; Params)
            paramValues[i] = s.getParam!(paramType)(i);

        // Call the function with the params (natively!)
        static if(is(ReturnType == void))
        {
            func(paramValues);
            return 0;
        }
        else
        {
            s.push(func(paramValues));
            return 1;
        }
    }

    // actually create the closure and set it as a global
    void bind(MDState s)
    {
        s.setGlobal(name, new MDClosure(s, &identifier("bound_" ~ 
nameof(func)), name));
    }
}

...

void foo(int x, int y)
{
    writefln(x, y);
}

bind!(foo);

AAAAA

I wish. 





More information about the Digitalmars-d mailing list