Update example code (saved as `hashy.nim`):
import std/[tables, hashes]
type
Value = ref object
i: int
proc `==`*(a: Value, b: Value): bool =
result = a.i == b.i
echo "Used our =="
proc hash*(a: Value): Hash =
result = hashes.hash(a.i)
echo "Used our hash for " & $(a.i)
var a: Table[Value, Value]
proc getIndex*(k: Value): int =
if a.hasKey(k):
return a[k].i
else:
return -1
proc setIndex*(k: Value, v: int) =
a[k] = Value(i: v)
a[Value(i:100)] = Value(i:300)
assert getIndex(Value(i:300)) == -1
assert getIndex(Value(i:100)) == 300
setIndex(Value(i:3), 400)
assert getIndex(Value(i:3)) == 400
Run
Compiled with `nim c hashy.nim`.
Running it gives me:
Used our hash for 100
Used our hash for 3
Run
So, my conclusion is that our custom hash function is only getting called for
direct index-assignments, e.g. `a[k] = X`