www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7779] New: D1-style opWhatever method is chosen in preference to opBinary under D2


           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


`The expression:

a op b

is rewritten as both:


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) {
        return yuiop;

    Qwert opAdd(Qwert yuiop) {
        return this;

void main() {
    Qwert asdfg = new Qwert;
    asdfg = asdfg + asdfg;
(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: -------
Mar 25 2012