As I mentioned, the whole premise of what you're doing is incorrect, so solving 
the problems you are facing won't actually solve your problem. Instead try this:
    
    
    import tables
    
    type MyObj = ref object
      age: int
    
    var testTable: Table[string, MyObj]
    
    proc populateTable() =
      testTable["key1"] = MyObj(age: 42)
      testTable["key2"] = MyObj(age: 32)
    
    proc checkTable() =
      # These lookups returns a reference (pointer) to the object stored 
somewhere on the heap
      echo testTable["key1"].repr
      echo cast[int](testTable["key1"].addr) # Cast to int so it can be printed
      echo testTable["key2"].repr
      echo cast[int](testTable["key2"].addr) # Cast to int so it can be printed
      
      for key in ["key1", "key2"]:
        var obj = testTable[key] # Obj now holds a reference to the object
        obj.age += 1 # Manipulate the object through the pointer
      
      # These still return the same reference, but we can see the data has been 
manipulated
      echo testTable["key1"].repr
      echo cast[int](testTable["key1"].addr) # Cast to int so it can be printed
      echo testTable["key2"].repr
      echo cast[int](testTable["key2"].addr) # Cast to int so it can be printed
    
    populateTable()
    checkTable()
    
    
    Run

Reply via email to