[Issue 5519] Saner struct equality

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 3 04:32:51 PST 2011


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



--- Comment #4 from Denis Derman <denis.spir at gmail.com> 2011-02-03 04:30:32 PST ---
One (hopefully last) more point:

A situation where one may constantly need to compare structs for equality (by
value!) is unittests:

unittest {
    ...
    assert (result == S(a, b));
}

This is actually how I stepped on the present issue.

The general workaround is indeed to write custom opEquals methods which just
compare member per member. Thus, I ended up adding such opEquals to all
structs:

struct Lexeme {
    string tag;
    string slice;
    Ordinal index;          // for parser match error messages
    string toString () {
        return format(`Lexeme("%s","%s",%s)`, tag,slice,index);
    }
    // workaround for bug in default struct '=='
    const bool opEquals (ref const(Lexeme) l) {
        return (
               this.tag == l.tag
            && this.slice == l.slice
            && this.index == l.index
        );
    }
}

Denis

-- 
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