A tiny-bit more elaborate example (based on your example).
    
    
    import std/[tables, hashes]
    
    type
        Value = ref object
            i: int
    
    var a: Table[Value, Value]
    
    proc hash(a: Value): Hash
    
    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)
    
    proc hash(a: Value): Hash =
      result = hashes.hash(a.i)
      echo "Used our hash"
    
    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

I see "Used our hash" twice. And it seems to me rather weird. Doesn't `hasKey` 
(in `getIndex`) hash the value first before looking it up? Or... doesn't 
`setIndex` hash the value before setting it?

So, I would expect at least 3 calls to the hash function (or perhaps 5, 
considering the 2 set-indexes)

Reply via email to