And i would like to share a macro for same pattern:
    
    
    import macros
    import tables
    
    
    macro ctor*(none: untyped): auto =
      let args = callsite()
      if args[1].kind != nnkProcDef:
        error("`ctor` pragma is used only with procedures.")
      var prc = args[1]
      
      if $(if prc[0].kind == nnkPostfix: prc[0][1] else: prc[0]) notin ["new", 
"init"]:
        error("Constructor must be named `new` if it returns a ref type or 
`init` if it returns a value type.")
      
      var params = prc[3]
      var resultType = params[0]
      
      var typeParam = newNimNode(nnkIdentDefs).add(
        newIdentNode("_"),
        newNimNode(nnkBracketExpr).add(
          newIdentNode("typedesc"),
          resultType.copyNimTree()
        ),
        newNimNode(nnkEmpty)
      )
      params.insert(1, typeParam)
      result = newStmtList(prc)
    
    
    proc init*[A, B](initial_size=64): Table[A, B] {.ctor, inline.} = 
init_table[A, B](initial_size)
    
    
    discard Table[int, int].init()
    

You delcare init or new proc with ctor pragma and it inserts first typedesc 
parameter automatically based on return type of the proc. Works with templates 
and everything. 

Reply via email to