Did you put the hash after or before the procedures that indexed the table? The
following code works as intended but as soon as you move the value below the
`set` and `get` it fails of course.
import std/[tables, hashes]
proc hash(a: int): Hash =
result = hashes.hash(a)
echo "Used our hash"
var a: Table[int, int]
proc getIndex*(k: int): int =
if a.hasKey(k):
return a[k]
else:
return -1
proc setIndex*(k: int, v: int) =
a[k] = v
a[100] = 300
assert getIndex(300) == -1
assert getIndex(100) == 300
setIndex(3, 400)
assert getIndex(3) == 400
Run
- Enforcing type safety for custom Hash function drkameleon
- Enforcing type safety for custom Hash function ElegantBeef
- Enforcing type safety for custom Hash function drkameleon
- Enforcing type safety for custom Hash function drkameleon
- Enforcing type safety for custom Hash functio... ElegantBeef
- Enforcing type safety for custom Hash fu... drkameleon
- Enforcing type safety for custom Has... drkameleon
- Enforcing type safety for custom... drkameleon
- Enforcing type safety for custom... ElegantBeef
