[Issue 17206] [Tracking] Check that opEquals and toHash are both defined or neither are defined

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 3 11:46:01 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=17206

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #3 from Steven Schveighoffer <schveiguy at yahoo.com> ---
No, toHash doesn't necessarily have to follow the semantics of default
opEquals.

Example:

struct S1(bool customTohash)
{
    string s;
    static if(customTohash)
    {
        size_t toHash() const
        {
            return cast(size_t)s.ptr + s.length; // use string identity
        }

        /+ really should define this
        bool opEquals(const(S1) other) const { return other.s is s; }
        +/
    }
}

void main()
{
    bool[S1!false] aa1;
    aa1[S1!false("hello".idup)] = true;
    aa1[S1!false("hello".idup)] = true;
    assert(aa1.length == 1);

    bool[S1!true] aa2;
    aa2[S1!true("hello".idup)] = true;
    aa2[S1!true("hello".idup)] = true;
    assert(aa2.length == 1); // fails
}

--


More information about the Digitalmars-d-bugs mailing list