You need to use generics.
    
    
    import tables
    
    proc getTableOf*[T]: ref Table[int, T] =
      var table {.global.} = newTable[int, T]()
      return table
    
    proc getVal[T](key: int): T =
      getTableOf[T]()[key]
    
    getTableOf[bool]()[0] = true
    assert getVal[bool](0) == getTableOf[bool]()[0]
    assert getVal[bool](0)
    
    proc getMutableVal[T](key: int): var T =
      getTableOf[T]()[key]
    
    getTableOf[bool]()[0] = true
    assert getVal[bool](0)
    getMutableVal[bool](0) = false
    assert(not getVal[bool](0))
    

If you still want to pass a typedesc as an argument, you can use typedesc[T].
    
    
    import tables
    
    proc getTableOf*[T](desc: typedesc[T]): ref Table[int, T] =
      var table {.global.} = newTable[int, T]()
      return table
    
    proc getVal[T](key: int, desc: typedesc[T]): T =
      getTableOf(T)[key]
    
    getTableOf(bool)[0] = true
    assert 0.getVal(bool) == getTableOf(bool)[0]
    assert 0.getVal(bool)
    
    proc getMutableVal[T](key: int, desc: typedesc[T]): var T =
      getTableOf(T)[key]
    
    getTableOf(bool)[0] = true
    assert 0.getVal(bool)
    0.getMutableVal(bool) = false
    assert(not getVal(0, bool))
    

Keep in mind you can also turn getVal and getMutableVal into templates instead 
of procs to get rid of runtime overhead. Templates don't need type information, 
they transform AST into other AST during compile time (from my knowledge at 
least).

Reply via email to