[Issue 12778] New: Aliasing opBinaryRight to opBinary works only in certain cases

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed May 21 02:32:31 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=12778

          Issue ID: 12778
           Summary: Aliasing opBinaryRight to opBinary works only in
                    certain cases
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: andrej.mitrovich at gmail.com

I've discovered a "feature" today where I can simply alias opBinaryRight to
opBinary and it will work. However it stops working when 'const' is involved.
Observe:

-----
struct Vec2
{
    Vec2 opBinary(string op)(Vec2 b) const
        if (op == "+" || op == "-" || op == "*")
    {
        mixin("return Vec2(this.x " ~ op ~ " b.x, this.y " ~ op ~ " b.y);");
    }

    Vec2 opBinary(string op)(float s) const
        if (op == "+" || op == "-" || op == "*")
    {
        mixin("return Vec2(s " ~ op ~ " this.x, s " ~ op ~ " this.y);");
    }

    alias opBinaryRight = opBinary;

    float x = 0, y = 0;
}

void main()
{
    Vec2 vec;
    auto v1 = vec + 1;  // ok
    auto v2 = 1 + vec;  // ok, opBinaryRight alias works!

    struct S
    {
        void test1()
        {
            Vec2 v = v1 - v2;  // ok
        }

        void test2() const
        {
            Vec2 v = v1 - v2;  // L37: error
        }

        Vec2 v1, v2;
    }
}
-----

test.d(37): Error: overloads const pure nothrow @nogc @safe Vec2(Vec2 b) and
const (Vec2 b) both match argument list for opBinary

I don't know if supporting this kind of aliasing was a feature by design or by
mistake. It's definitely nice to have it, but it fails in 'test2'. It should
either be fully supported (rejects-valid) or fully rejected (accepts-invalid).

--


More information about the Digitalmars-d-bugs mailing list