This doesn't seem to work. 
    
    
    when compiles(proc hello() = echo "hi"):
      proc hello() = echo "hi"
    
    
    Run

but passing the code into a macro does. 
    
    
    import macros
    macro tester(): untyped =
      let procDef = nnkProcDef.newTree(
        ident("hello"),
        newEmptyNode(),
        newEmptyNode(),
        nnkFormalParams.newTree(
          newEmptyNode()
        ),
        newEmptyNode(),
        newEmptyNode(),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("hi")
          )
        )
      )
      var whenStmt = nnkWhenStmt.newTree(
        nnkElifBranch.newTree(
          nnkCall.newTree(
            newIdentNode("compiles"),
            nnkStmtListExpr.newTree(procDef)
          )
        )
      )
      #treeRepr looks identical to the quote do version
      #but it will fail
      var whenStmt2 = nnkWhenStmt.newTree(
        nnkElifBranch.newTree(
          nnkCall.newTree(
            newIdentNode("compiles"),
            nnkStmtListExpr.newTree(procDef)
          ),
          newStmtList(procDef,nnkCommand.newTree(ident"echo",newLit("bye")))
        )
      )
      whenStmt[0].add quote do:
        proc hello() =
          echo "hi"
        echo "bye"
      #result = newStmtList(procDef)
      result = newStmtList(whenStmt)
      echo treeRepr result
      echo repr result
      echo treeRepr whenStmt2
    
    tester()
    hello()
    
    
    Run

oddly enough if you use whenStmt2 instead of the quote do version proc hello 
won't exist so that's another problem altogether

Reply via email to