Bug 3967 – TDPL bool opEquals() for structs instead of int opEquals()
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-15T10:23:00Z
Last change time
2014-02-15T02:43:29Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-03-15T10:23:12Z
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 by bearophile_hugs — 2010-03-18T14:30:25Z
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 by lutger.blijdestijn — 2010-08-07T03:13:55Z
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 by bearophile_hugs — 2010-08-07T03:20:00Z
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 by lutger.blijdestijn — 2010-08-07T04:42:33Z
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.