www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20486] New: D1 style operator overloading should not be


          Issue ID: 20486
           Summary: D1 style operator overloading should not be preferred
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

D2 operators are overloaded via opUnary, opBinary, opBinaryRight, etc. However,
D1 style operators have been supported for some time. In 2.088, D1 style
operators were deprecated, which means compiling with deprecations as errors
produces errors for D1 style operators.

As always with D, operator overloading is done via normal functions named a
certain way. It's not deprecated to create a method named, e.g. opIn_r. But
calling with operator syntax is.

Because they are normal functions, and because templates cannot be virtual, the
rationale for introducing these D2-style operators in a way that could be
backwards compatible was that you can still define the D1 operators as virtual
functions, and then provide thin wrappers over them as D2-style operator
overloads. In fact, you can do aliases directly to the functions!

But if you turn on deprecations as errors, this does not work, because the
compiler prefers the D1-style operator overloading.


class C
    void opIn_r(int x)

    void opBinaryRight(string s : "in")(int x)

void main()
    auto c = new C;
    5 in c;

This produces a deprecation error, the opBinaryRight function is not used at

This does not provide a path to have code that builds between successive
compilers when we remove the D1 style operators. Note that there is no good
reason to require the virtual component have a different name than the original
D1 style operators. In fact, it was presented that one could easily provide a
mixin that generated all the wrappers.  But this will not be possible until the
deprecations and hooking via the D1 operators are removed.

I propose the compiler should prefer the D2-style operator overloading if
present, and then fall-back to the D1 style (and produce a deprecation message)
if that does not work. Only deprecate the binding of operators to the D1 style
methods, not calling the methods directly. This will provide a migration path.

Jan 06 2020