Yes, that's how Dictionary
is implemented now. But my question is why it has to be in that way and isn't it too much berden on the user to make sure ==
and hashValue
don't contradict each other.
I've experience the problem when I imported a 3rd party framework that happened to be implementing Equatable
to the type of which I was using for a dictionary key. I implemented hashValue
and ==
to the type, but the compiler didn't warn me there were two ==
functions to the type, and I needed to realize my ==
method wasn't called at runtime to figure out the bug.
Anyway, when two objects are not equal but have the same hashValue
, what behavior should a user reasonably expect?
struct Foo { let n }
extension Foo: Equatable {
static func == (lhs: Foo, rhs: Foo) -> Bool { return rhs.n == lhs.n }
}
extension Foo: Hashable {
var hashValue: Int { return n / 2 }
}
var dict1 = [Foo: Int]()
dict1[Foo(n: 2)] = 2
dict1[Foo(n: 3)] = 3
print("\(dict1.count)") // I was expecting this to be 1.