Bug 7470 – opEquals for interfaces

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-08T23:41:44Z
Last change time
2024-12-13T17:58:21Z
Assigned to
No Owner
Creator
Martin Nowak
Moved to GitHub: dmd#18413 →

Comments

Comment #0 by code — 2012-02-08T23:41:44Z
interface I { int value(); equals_t opEquals(I other); // final opEquals should be allowed too // final equals_t opEquals(I other) { return value() == other.value(); } } class A : I { override int value() { return 0; } override equals_t opEquals(I other) { return value() == other.value(); } } class B : I { override int value() { return 0; } override equals_t opEquals(I other) { return value() == other.value(); } } void main() { I i1 = new A, i2 = new B; assert(i1 == i2); } ---- I think this was the actual issue that Steven filed under bug 4088. Using explicit casts solved compiler generated interface comparison, but doesn't allow a real opEquals for interfaces. Note that opCmp works as expected.
Comment #1 by schveiguy — 2012-02-09T07:36:48Z
The original test case was that I could not compare two interfaces in the way you say, yes. But you can compare two interfaces via Object: interface I { } class C : I { } void main() { I i1 = new C, i2 = new C; assert(i1 == i2); // should work now, did not work before } Prior to the fix, you could not compare two interfaces under *any circumstances*. This at least allows interfaces to pull in Object's method for opEquals. What you are asking for is for interfaces (and by extension objects) to be comparable to other objects using a derived type, which I think could be a useful addition, but complex in implementation. You don't want to get into a situation where two objects compare differently depending on the static type used.
Comment #2 by robert.schadek — 2024-12-13T17:58:21Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18413 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB