Jump to page: 1 2
Thread overview
[Issue 16657] alias this interacts with generated opCmp and opEquals
[Issue 16657] [The D Bug Tracker]
Nov 02, 2016
Eyal
May 13, 2017
Walter Bright
Jun 19, 2017
Eyal
Jun 19, 2017
Eyal
Jun 30, 2017
Eyal
Jun 30, 2017
ZombineDev
Jan 24, 2019
RazvanN
Jan 29, 2019
Eyal
Mar 01, 2019
Dlang Bot
Apr 09, 2019
Dlang Bot
Jul 28, 2020
Dlang Bot
November 02, 2016
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #1 from Eyal <eyal@weka.io> ---
Minor correction: opCmp is not auto-generated anyway and opEquals should just be exhaustive and not lexicographic.

--
December 22, 2016
https://issues.dlang.org/show_bug.cgi?id=16657

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@erdani.com
            Summary|[The D Bug Tracker]         |alias this interacts with
                   |                            |generated opCmp and
                   |                            |opEquals

--
May 13, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> ---
Since the auto-generated opEquals() theoretically always exists, having it take precedence makes it a problem to wrap other types and defer to it. But the problem does come in when the auto-generated opEquals() would be non-trivial, as in:

---
struct A {
    int x;
    bool opEquals(int y) { return y == x; }
}

struct C {
    int a;
    A b;
    alias a this;
}

static assert(C(1, A(1)) != C(1, A(2)));
---

The current state of affairs is that you'll need to write an explicit C.opEquals() if using an 'alias this' and do not wish the operation to be forwarded to it, even if the other fields have non-trivial opEquals() implementations.

I suspect that this is the best solution if only because it is easy to understand. It doesn't have any special cases and exceptions.

--
May 13, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #3 from Andrei Alexandrescu <andrei@erdani.com> ---
It seems a case can be made for either behavior.

* The "alias this" feature emulates subtyping of the kind typically achieved by class inheritance. If that is the default behavior to emulate, the current behavior is up to the task. Consider a moral equivalent:

class A {
    int x;
    this(int x) { this.x = x; }
    override bool opEquals(Object rhs) { return x == (cast(A) rhs).x; }
}

class B : A {
    this(int x, int y) { super(x); this.y = y; }
    int y;
}

void main() {
    assert(new A(1) == new B(1, 2)); // pass
}

This "slices" B for the purpose of comparison.

* Yes, "alias this" does emulate subtyping, but we have an autogenerated opEquals for every struct. Why should there be the case that such generation automatically disappears in the case "alias this" is used?

* Another argument for changing behaviors has to do with practicality. Even
though a subtyping-based argument can be created in favor of preserving
existing compatibility, a better argument is that in fact the _current_ OOP
behavior (illustrated by the example with classes above) is incorrect and
error-prone. There's good evidence for that (e.g.
http://stackoverflow.com/questions/12239344/why-should-i-not-use-equals-with-inheritance
of many examples). The theoretical underpinnings for correctly defining
comparison in conjunction with subtyping is bounded quantification
(https://en.wikipedia.org/wiki/Bounded_quantification).

The short of it all is the current behavior can and should be improved. Changing behavior silently is not a good choice, so we should probably issue a warning when the situation occurs. The warning can be extinguished by defining opEquals explicitly. With time the warning becomes an error following a schedule similar to deprecation.

--
June 19, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #4 from Eyal <eyal@weka.io> ---
I just spent another half a day debugging a very cryptic bug that crashed our QA runs, and resulted from an incorrect comparison of just an inner field that had "alias this" :-(

--
June 19, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #5 from Eyal <eyal@weka.io> ---
We had something like this:

    if(x != y) {
      x = y; // expensive assignment
    }
    // assume all x fields equal y fields here, we assign them all -- ouch!

The semantics now are very surprising:

  x = y // assigns the whole of "x", not just the "alias this" part
  x == y // compares just the "alias this" part, wat?

Well-behaved assignments and equalities should apply to the same part of the object. That the auto-generated assignment/equals code is NOT well-behaved is surely to be considered a bug.

Implementation-wise:

It is quite simple to always auto-generate opEquals that compares all fields with == recursively calling opEquals. The auto-generated opEquals overrides the "alias this" opEquals for the (Subtype,Subtype) case. I don't see where the implementation difficulty comes in here.

In Walter's example, auto-gen'd C.opEquals would use == on both fields, implicitly invoking opEquals of A. No special cases or exceptions.

--
June 30, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #6 from Andrei Alexandrescu <andrei@erdani.com> ---
Upon further consideration we need to fix this. Requiring people who use the "alias this" feature to also remember to define "opEquals" is the kind of long-distance dependency that is the subject of "Effective D" tidbits of advice of the exact kind we do our best to avoid.

--
June 30, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

--- Comment #7 from Eyal <eyal@weka.io> ---
Awesome, thanks!

--
June 30, 2017
https://issues.dlang.org/show_bug.cgi?id=16657

ZombineDev <petar.p.kirov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |petar.p.kirov@gmail.com

--
January 24, 2019
https://issues.dlang.org/show_bug.cgi?id=16657

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #8 from RazvanN <razvan.nitu1305@gmail.com> ---
PR: https://github.com/dlang/dmd/pull/9289

--
« First   ‹ Prev
1 2