Thread overview
[Issue 3967] New: bool opEquals() for structs instead of int opEquals()
Aug 07, 2010
Lutger
Aug 07, 2010
Lutger
[Issue 3967] TDPL bool opEquals() for structs instead of int opEquals()
March 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3967

           Summary: bool opEquals() for structs instead of int opEquals()
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-03-15 10:23:12 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3967



--- Comment #1 from bearophile_hugs@eml.cc 2010-03-18 14:30:25 PDT ---
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));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3967


Lutger <lutger.blijdestijn@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lutger.blijdestijn@gmail.co
                   |                            |m


--- Comment #2 from Lutger <lutger.blijdestijn@gmail.com> 2010-08-07 03:13:55 PDT ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3967



--- Comment #3 from bearophile_hugs@eml.cc 2010-08-07 03:20:00 PDT ---
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);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3967



--- Comment #4 from Lutger <lutger.blijdestijn@gmail.com> 2010-08-07 04:42:33 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 11, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3967


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrei@metalanguage.com
         Resolution|                            |FIXED


--- Comment #5 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-12-11 12:35:53 PST ---
https://github.com/D-Programming-Language/d-programming-language.org/commit/086d56d031bbdbf5c96abd83ba51b7986bfcb2a9

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------