[Issue 13179] AA key type TagIndex now requires equality rather than comparison

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jul 23 17:46:47 PDT 2014


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

--- Comment #16 from Jonathan M Davis <jmdavisProg at gmx.com> ---
How about this, the programmer has to declare opEquals if they define opCmp (in
order to force the programmer to be aware of the problem and choose how
opEquals should work), but we provide a way to tell the compiler to use the
default opEquals rather than forcing the programmer to define their own. e.g.
something like

struct S
{
    bool opEquals(S s) = default;
    int opCmp()(auto ref S s) {...}
}

The same with toHash so that the programmer doesn't have to define that just
because they defined opEquals in the cases where the default would work.

I believe that C++11 did something like this. By doing this, we force the
programmer to consider it but don't force them to define opEquals or toHash
themselves when the defaults should work.

Regardless, if the programmer wants opEquals to just call opCmp, I think that
they should have to define that themselves, since having the compiler do it
would incur a silent performance hit. At least the definition is short and
easy:

bool opEquals()(auto ref S s) { return opCmp(s) == 0; }

--


More information about the Digitalmars-d-bugs mailing list