On Sunday, 3 May 2015 at 18:54:45 UTC, Andrei Alexandrescu wrote:
You're right, that is lovely! I've improved it as follows:

mixin template forwardToMember(alias member, methods...)
{
    import std.algorithm : among;
    import std.traits : ParameterTypeTuple;
    template opDispatch(string sym)
    if ((methods.length == 0 || sym.among(methods)))
    {
        auto ref opDispatch(
ParameterTypeTuple!(__traits(getMember, member, sym)) args)
        {
            return __traits(getMember, member, sym)(args);
        }
    }
}

So now ref returns are preserved and the mixin is self-contained (doesn't require imports from the outside).

Compared to my solution, this has the advantage that if the child defines a method, it will take precedence over the formatted one. So that allowed me to add a feature: if no methods are specified, all are forwarded.

There are a couple of ways in which this could and should be improved, most notably overloads control. Even as is it's pretty darn awesome, Meta could you please make it into a pull request? I think it should go in std.functional.


Andrei

I have forgotten to mention that I will make a pull request for this ASAP. I have been in the process of moving and starting a new job this week and last so I only have enough time to occasionally post currently.

Reply via email to