[Issue 18678] std.datetime.date.TimeOfDay has opCmp but no toHash

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 28 17:24:06 UTC 2018


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

--- Comment #3 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
(In reply to Jack Stouffer from comment #2)
> If opCmp is defined but opEquals isn't, doesn't DMD re-write `val == val` to
> `val.opCmp(val) == 0`?

No. The only way to override == and != is opEquals. e.g. this code runs and
passes just fine:

    struct S
    {
        int opCmp(S rhs)
        {
            assert(0);
        }
    }

    void main()
    {
        assert(S.init == S.init);
    }

If opCmp is consistent with the default opEquals, then there is no need to
define opEquals, and opCmp will never be called for == or !=. If opCmp is not
consistent with the default opEquals, then opEquals needs to be defined. And if
opEquals is defined, then toHash needs to be defined. But as long as opEquals
isn't defined, then the default toHash should work just fine.

--


More information about the Digitalmars-d-bugs mailing list