March 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7779

           Summary: D1-style opWhatever method is chosen in preference to
                    opBinary under D2
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: smjg@iname.com


--- Comment #0 from Stewart Gordon <smjg@iname.com> 2012-03-25 15:07:57 PDT ---
DMD 2.058, Win32

http://dlang.org/operatoroverloading.html#Binary

`The expression:

a op b

is rewritten as both:

a.opBinary!("op")(b)
b.opBinaryRight!("op")(a)

and the one with the ‘better’ match is selected. It is an error for both to equally match.`

No mention of opAdd, etc. in there.  Nonetheless, if such a method is present, it is chosen instead of behaving according to spec:
----------
import std.stdio;

class Qwert {
    Qwert opBinary(string op : "+")(Qwert yuiop) {
        puts("opBinary");
        return yuiop;
    }

    Qwert opAdd(Qwert yuiop) {
        puts("opAdd");
        return this;
    }
}

void main() {
    Qwert asdfg = new Qwert;
    asdfg = asdfg + asdfg;
}
----------
C:\Users\Stewart\Documents\Programming\D\Tests>opbinary_opadd opAdd
----------
(DMD 2.058 Win32)

The spec gives opBinary/opBinaryRight as the way the AddExpression is resolved; yet the compiler does something else instead.

It might be reasonable as as backward compatibility measure for the opAdd to be used as a fallback if there's no matching opBinary or opBinaryRight.  But it's undocumented behaviour.  When a matching opBinary is present, by allowing an undocumented feature to override a documented one the compiler is going against the spec.

Swapping the order opBinary and opAdd in the code doesn't change the behaviour.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------