Factor code, maintaining readability and improving life :)

I am just starting to learn the D programming language and am happily playing with the keywords of the delegate and function. I am trying to implement (only for my own learning process) the well-known twisted Deferred class (written in Python).

I am just wondering if there are any tricks to drop such code:

class Deferred(ResultType)
{
    alias ResultType delegate(ResultType) CallbackType;

    private CallbackType[2] _callbacks[];

    private bool _running;
    private uint _paused;

    void addCallback(CallbackType cb)
    {
        this._callbacks ~= [cb, cast(CallbackType) null];
    }

    void addCallback(void function() f)
    {
        this.addCallback(
            (ResultType res) { f(); return res; }
        );
    }

    void addCallback(void function(ResultType) f)
    {
        this.addCallback(
            (ResultType res) { f(res); return res; }
        );
    }

    void addCallback(ResultType function() f)
    {
        this.addCallback(
            (ResultType res) { return f(); }
        );
    }

    void addCallback(ResultType function(ResultType) f)
    {
        this.addCallback(
            (ResultType res) { return f(res); }
        );
    }

}

The goal is to allow the user not to pass a delegate CallbackType, but some function, with or without the correct type of argument / return. Did I miss some important point here?

[EDIT]: with Mehrdad's advice, the answer might be something like this:

class Deferred(ResultType)
{
    alias ResultType delegate(Deferred, ResultType) CallbackType;

    private CallbackType[2] _callbacks[];

    void addCallback(T)(T cb)
    {
        this._callbacks ~= [this._makeConvenient(cb), cast(CallbackType) null];
    }

    private CallbackType _makeConvenient(T)(T f)
    {
        alias traits.ReturnType!(f) ReturnType;
        alias traits.ParameterTypeTuple!(f) Params;

        return (Deferred d, ResultType res)
        {
            ReturnType wrapper()
            {
                static if (Params.length == 2)
                {
                    static if (is(Params[0] == Deferred!(ResultType)) && is(Params[1] == ResultType))
                        return f(this, res);
                    else
                        static assert(false, "Cannot wrap given callback: Wrong arguments types");
                }
                else static if (Params.length == 1)
                {
                    static if (is(Params[0] == Deferred!(ResultType)))
                        return f(this);
                    else static if (is(Params[0] == ResultType))
                        return f(res);
                    else
                        static assert(false, "Cannot wrap given callback: Wrong argument type");
                }
                else static if (Params.length == 0)
                    return f();
                else
                    static assert(false, "Cannot wrap given callback: Wrong argument number");
            }

            static if (is(ReturnType == void)) { wrapper(); return res; }
            else static if (is(ReturnType == ResultType)) { return wrapper(); }
            else static assert(false, "Cannot wrap given callback: Wrong return type");
        };
    }
}

Am i right? Are there any noticeable performance issues?

+3
source share
1 answer

This should do it:

class Deferred(ResultType)
{
    alias ResultType delegate(ResultType) CallbackType;

    private CallbackType[2] _callbacks[];

    private bool _running;
    private uint _paused;

    void addCallback(CallbackType cb)
    {
        this._callbacks ~= [cb, cast(CallbackType) null];
    }

    void addCallback(TFn)(TFn f) //Put template restrictions here if you want
    {
        this.addCallback((ResultType res)
        {
            static if (is(typeof(f(res)) == void)) { f(res); return res; }
            else static if (is(typeof(f()) == void)) { f(); return res; }
            else static if (is(typeof(f(res) == ResultType))) { return f(res); }
            else { return f(); }
        });
    }
}

It is not much better, but everything is fine, I think.

+4
source