[Issue 18615] New: Rebindable!A doesn't use class A's opEquals (returns a is b instead)
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Mar 15 04:57:15 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=18615
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 at puremagic.com
Reporter: eiderdaus at 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.
#!/usr/bin/rdmd
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
--
More information about the Digitalmars-d-bugs
mailing list