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.
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)); }
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?
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); }
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.
https://github.com/D-Programming-Language/d-programming-language.org/commit/086d56d031bbdbf5c96abd83ba51b7986bfcb2a9