1.0 ?? [templates and currying]

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Nov 6 19:45:21 PST 2006


Jarrett Billingsley wrote:
> "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. 
> 
> 

... have you been reading Pyd's function wrapping code? ;-)

The internal code isn't /quite/ that clean, but the end result (the def 
template) is.

http://www.dsource.org/projects/pyd/browser/trunk/infrastructure/pyd/func_wrap.d

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

extern(C)
export void inittestdll() {
     def!(foo);
     module_init("testdll");
}

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list