[Issue 3789] Structs members that require non-bitwise comparison not correctly compared

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 26 04:46:03 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=3789



--- Comment #16 from Steven Schveighoffer <schveiguy at yahoo.com> 2012-03-26 04:46:26 PDT ---
(In reply to comment #15)
> This is an answer to Walter to Bug 7783 :
> 
> (In reply to comment #2)
> > In the absense of a user-defined opEquals for the struct, equality is defined
> > as a bitwise compare. This is working as expected. Not a bug.
> 
> I agree, it's not a DMD implementation bug because here it's working as
> designed.

The statement above is not completely true:

struct S
{
    string x;
    bool opEquals(const ref S other) const
    {
        return x == x;
    }
}

struct T
{
    S s;
    // no user-defined opEquals
}

void main()
{
    T t1, t2;
    t1.s.x = "foobar".idup;
    t2.s.x = "foobar".idup;
    assert(t1 == t2);
}

This passes in 2.058.  Essentially, bitwise comparison is done unless a member
requires special case opEquals, in which case a generated opEquals function is
made for that struct.

I contend that for the compiler to defer to user-defined structs, but not to
builtin types on how to compare themselves is not only inconsistent, but leads
to endless boilerplate code.  It does not help anyone.

We have 'is', which does a bitwise compare.  There is no reason to make ==
duplicate that functionality.  For the rare cases where you actually *need*
bitwise comparison on arrays or floats, you can define a special opEquals.

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


More information about the Digitalmars-d-bugs mailing list