On Tuesday, 3 October 2017 at 19:25:32 UTC, Walter Bright wrote:

This gets into the anti-hijacking support D has (and C++ does not). If multiple modules with declarations of `writeln` are in scope, the compiler attempts a match with `writeln` in each module. If there is a match with more than one module, an error is issued. The user will then have to specify which one he wants.


Anti-hacking might make it tricky to have operators as free-standing functions, but I'm not sure it would be impossible. The solution for function overloading is fully qualified names. However, this does not make sense operator overloading. Mixing operator overloading and D's anti-hijacking, you would basically be prevented from having two overloaded versions of an operator. This means that if you want multiple versions of operators, you would not be able to have the operators as member functions and they would have to be free-standing functions, ideally in separate modules.

With respect to the earlier discussion of type wrappers, this can also be implemented with a template parameters that controls the operator-overloading of *, something like below.

struct Foo(bool mtimesOverload = false)
{
    template opBinary(string op = "*")(Foo foo)
    {
        static if (mtimesOverload)
        {
            return mtimes(this, foo);
        }
        else
        {
            return times(this, foo);
        }
    }
}

So you could do a Matrix alias that has mtimesOverload=true.

Reply via email to