[Issue 13114] old opCmp requirement for AA keys should be detected for classes

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jul 12 13:02:19 PDT 2014


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

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code at dawg.eu,
                   |                            |hsteoh at quickfur.ath.cx

--- Comment #1 from Steven Schveighoffer <schveiguy at yahoo.com> ---
Further update, I think there is a valid case for allowing redefining of opCmp
and not opEquals -- if you are not changing the equality portion of opCmp.

For instance:

class C
{
    this(int x, int y) pure {this.x = x; this.y = y;}
    int x;
    int y;
    override bool opEquals(Object other) { if(auto o = cast(C)other) {return x
== o.x;} return false;}
    override int opCmp(Object other) { if(auto o = cast(C)other) {return x <
o.x ? -1 : x > o.x ? 1 : 0;} return -1;}
    override hash_t toHash() const { return x; }
    override string toString() const { return "{" ~ x.to!string ~ ", " ~
y.to!string ~ "}";}
}

class ReverseC : C
{
    override int opCmp(Object other) { if(auto o = cast(C)other) {return x >
o.x ? -1 : x < o.x ? 1 : 0;} return -1;}
}

ReverseC should be able to be an AA key, because the opCmp calculation for
equality did not change. This can be an AA key under 2.065, and under the new
compiler/druntime.

I think we cannot actually fix this issue. I don't know what to do here. CC'ing
major players on this issue to make sure it gets noticed.

--


More information about the Digitalmars-d-bugs mailing list