www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18615] New: Rebindable!A doesn't use class A's opEquals


          Issue ID: 18615
           Summary: Rebindable!A doesn't use class A's opEquals (returns a
                    is b instead)
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: eiderdaus gmail.com

DMD64 D Compiler v2.079.0

The Phobos docs for Rebindable say: "Rebindable!(Widget) does allow
reassignment, while otherwise behaving exactly like a const Widget."
Source: https://dlang.org/library/std/typecons/rebindable.html

The following code violates "behaving exactly like a const Widget": Even though
class A overrides opEquals, the Rebindable!(const(A)) doesn't call A.opEquals.

    import std.typecons;

    class C {
        int x;
        override bool opEquals(Object rhsObj)
            const(C) rhs = cast(const(C)) rhsObj;
            return this.x == rhs.x;

    void main()
        C a = new C();
        C b = new C();
        assert (a == b); // OK: Even though a !is b, we overrode opEquals
                         // and compare a.x == b.x, which is true (0 == 0).

        Rebindable!(const(C)) ca = a;
        Rebindable!(const(C)) cb = b;
        assert (ca == cb); // Fails! struct Rebindable doesn't forward its
                           // opEquals to the class's opEquals!

Rebindable!(const(A)).opEquals seems to return true iff the references point to
the same const(A).

Is this a bug in Rebindable's implementation, or is this by design and should
be documented? If it's by design, that's very surprising though and has caught
me several times.

-- Simon

Mar 14 2018