Normally to do identifier construction of variable you need to use `{.inject.}`

However untyped macro will capture everything and you will have to deal with 
the inject pragma in your macro.

Instead forget about the template and do identifier construction directly in it.
    
    
    import macros
    
    
    static: echo "Target macro: "
    
    dumpASTgen():
      let xExtra = 1000
    
    static: echo "#############"
    
    macro defthing(name: untyped): untyped =
      echo name.treerepr
      result = newStmtList()
      
      result.add nnkLetSection.newTree(
        nnkIdentDefs.newTree(
          newIdentNode($name & "Extra"),
          newEmptyNode(),
          newLit(1000)
        )
      )
    
    defthing awesome
    
    
    echo "xExtra is: " & $awesomeExtra
    

Now building on your example, if you want to match normal Nim syntax and macro 
calls. Here are 3 ways to do that from within macros and templates:
    
    
    import macros
    
    macro defthing(name: untyped): untyped =
      echo name.treerepr
      result = newStmtList()
      
      result.add nnkLetSection.newTree(
        nnkIdentDefs.newTree(
          newIdentNode($name & "Extra"),
          newEmptyNode(),
          newLit(1000)
        )
      )
      
      # 1. Extra stuff in macro v1
      template extraThing(): untyped =
        echo "wow extra from template with normal Nim syntax"
      
      result.add getAST(extraThing())
      
      # 2. Extra stuff in macro v2"
      result.add quote do:
        echo "yet more thing to do in macro with normal Nim syntax"
    
    template yetyetAnother(name: untyped): untyped =
      
      # 3. Extra stuff in template
      defthing(name)
      echo xExtra * 1337
    
    
    yetyetAnother(x)
    

Reply via email to