Consider this struct
struct Node : Hashable {
let value : Int
let i : Int
let j : Int
init(withValue val : Int, position : (Int,Int)){
value = val
self.i = position.0
self.j = position.1
}
var hashValue: Int {
return "\(value),\(i),\(j)".hashValue
}
}
My ==
operator
func ==(left: Node, right: Node) -> Bool {
return left.hashValue == right.hashValue
}
When I create 2 nodes :
let node1 = Node(withValue: 1260, position: (8,694))
let node2 = Node(withValue: 33, position: (257,286))
And compare them :
node1 == node2 //true ???
Why is the hashValue
function not working as expected?
Should it be implemented in some different way?
Counter question : If yes, what is the right way to calculate hashValue for this kind of object?
More info
When I debugged this :
(lldb) po node1.hashValue
4799450060528192039
(lldb) po node2.hashValue
4799450060528192039