In [1] it says at 5. that

For this reason, and for legacy reasons, an associative array key is not allowed to define a specialized opCmp, but omit a specialized opEquals. This restriction may be removed in future versions of D.

I'm not completely sure what that means. Does "specialized" mean "user-defined"? I just challenged the spec and found an error by the way: [2]. Apart from that, it compiles.

For 5. I used

struct Key
{
    int id;
    string tag;

    int opCmp(const Key other) const
    {
return this.id < other.id ? -1 : this.id == other.id ? 0 : 1;
    }

    bool opEquals(ref const Key other) const @safe pure nothrow
    {
        return this.id == other.id;
    }

    size_t toHash() const @safe pure nothrow
    {
        return id;
    }
}

as a key type. To me the part "is not allowed to define a specialized opCmp" is clearly wrong, either a compiler bug or an error in the spec.

Concerning opEquals and opCmp in general: Why isn't opEquals lowered to opCmp returning 0 if not present?

[1] https://dlang.org/spec/hash-map.html#using_struct_as_key
[2] https://github.com/dlang/dlang.org/pull/1861

Reply via email to