D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10292 - Warn against wrong class opEquals signature usage
Summary: Warn against wrong class opEquals signature usage
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2013-06-07 14:42 UTC by bearophile_hugs
Modified: 2024-12-13 18:07 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-06-07 14:42:07 UTC
class Foo {
    int x;
    this(int x) {
        this.x = x;
    }
    bool opEquals(in Foo a) const {
        return a.x == this.x;
    }
}
void main() {
    Foo f1 = new Foo(10);
    Foo f2 = new Foo(10);
    assert(f1 != f2);
    assert(f1.opEquals(f2));
}


/*
This code compiles with no errors nor warnings with dmd 2.064alpha.

Expected something like:

test(6): Warning. Class opEquals should have signature like: override bool opEquals(Object) const


Here a little more correct opEquals is:

    override bool opEquals(Object a) const {
        auto fooa = cast(Foo)a;
        if (fooa is null)
            return false;
        return fooa.x == this.x;
    }


The compiler needs to give warnings or errors for such cases of wrong signatures. If you don't write opEquals(Object) it needs to complain. I think the current situation of silent accepting wrong/useless special methods is not acceptable in modern language.

(Code adapted from a post by "Namespace" in D.learn).
Comment 1 dlangBugzillaToGithub 2024-12-13 18:07:50 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18603

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB