On Thursday, 12 May 2016 at 10:38:37 UTC, Dsby wrote:

I write one, bind functon to a delegate.

In here:
https://github.com/putao-dev/collie/blob/master/source/collie/utils/functional.d


this is the code:

auto bind(T,Args...)(auto ref T fun,Args args) if (isCallable!(T))
{
    alias FUNTYPE = Parameters!(fun);
    static if(is(Args == void))
    {
        static if(isDelegate!T)
            return fun;
        else
            return toDelegate(fun);
    }
    else static if(FUNTYPE.length > args.length)
    {
        alias DTYPE = FUNTYPE[args.length..$];
        return
            delegate(DTYPE ars){
                TypeTuple!(FUNTYPE) value;
                value[0..args.length] = args[];
                value[args.length..$] = ars[];
                return fun(value);
            };
    }
    else
    {
        return delegate(){return fun(args);};
    }
}

Thank you. Would you agree to help me understand it ?

The only thing I don't understand is why the function template argument is defined as T and the argument as auto ref T fun. Why the auto ref and not alias T in the template argument list ?

This bind is better than Partial!() from std.functional since it accepts any number of parameters. But the given parameters are passed as first arguments of fun. The std::bind of C++ allows to bind any parameter in any order and eventually multiple times. It's really as if a new function was defined with a total liberty degree on its signature.

Anyway thank you very much.

Reply via email to