D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3967 - TDPL bool opEquals() for structs instead of int opEquals()
Summary: TDPL bool opEquals() for structs instead of int opEquals()
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2010-03-15 10:23 UTC by bearophile_hugs
Modified: 2014-02-15 02:43 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-03-15 10:23:12 UTC
This page:
http://www.digitalmars.com/d/2.0/operatoroverloading.html

Contains:

If structs declare an opEquals member function, it should follow the following form:

struct S {
    int opEquals(ref const S s) { ... }
}


But opEquals of classes returns a boolean, so I think it's better for opEquals of structs too to return a boolean. I think you must be sure opEquals returns a bool. So the specs can be changed into something like:

struct S {
    bool opEquals(ref const(S) s) { ... }
}


Currently this code runs:


import std.c.stdio: printf;
struct Foo {
    int data;
    int opEquals(T:Foo)(T other) {
        printf("A");
        return this.data == other.data;
    }
}
void main() {
    int r = Foo(5) == Foo(5);
}


But I think dmd has to raise a c error, and require something like:
bool opEquals(T:Foo)(ref const(Foo) other) {
Or:
bool opEquals(T:Foo)(const(Foo) other) {
etc.
Comment 1 bearophile_hugs 2010-03-18 14:30:25 UTC
This shows why requiring a bool as return value of opEquals is necessary for generic code too (adapted from an idea of Bill Baxter):


import std.c.stdio: printf;
struct Foo {
    int x;
    int opEquals(T:Foo)(T other) {
        printf("****\n");
        return this.x - other.x;
    }
}
bool bar(T)(T f1, T f2) {
    // return f1 == f2;  // ERR
    return !!(f1 == f2); // OK, but silly
}
void main() {
    bool r = bar(Foo(1), Foo(2));
}
Comment 2 Lutger 2010-08-07 03:13:55 UTC
dmd (2.047) requires a signature of 'bool opEquals(ref const S s) const' and the same requirement is stated in TDPL. I assume that this is now just an issue of updating the spec, should this bug be closed and a new one filed?
Comment 3 bearophile_hugs 2010-08-07 03:20:00 UTC
It's not a spec issue, this code compiles and runs with DMD 2.047 still:

import std.c.stdio: printf;
struct Foo {
    int data;
    int opEquals(T:Foo)(T other) {
        printf("A");
        return this.data == other.data;
    }
}
void main() {
    int r = Foo(5) == Foo(5);
}
Comment 4 Lutger 2010-08-07 04:42:33 UTC
Something else is going on, the following does fail in line with your proposal:

import std.c.stdio: printf;

struct Foo {
   int data;
   
   int opEquals(ref const(Foo) other) const 
   {
       
       printf("A");
       return this.data == other.data;
   }
}
void main() {
   int r = Foo(5) == Foo(5);
}

output: Error: function test.Foo.opEquals type signature should be const bool(ref const(Foo)) not const int(ref const(Foo) other)

Changing opEquals to return bool makes it compile correctly.