www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3967] New: bool opEquals() for structs instead of int opEquals()

reply d-bugmail puremagic.com writes:
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



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: -------
Mar 15 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3967




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: -------
Mar 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3967




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: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3967




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: -------
Aug 07 2010
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 11 2011