Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn against wrong class opEquals signature usage #18603

Open
dlangBugzillaToGithub opened this issue Jun 7, 2013 · 0 comments
Open

Warn against wrong class opEquals signature usage #18603

dlangBugzillaToGithub opened this issue Jun 7, 2013 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-06-07T14:42:07Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=10292

Description

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant