You can use a {.experimental: "ForLoopMacros".} to rewrite the for loop.

See for example the impelmentation of Python enumerate
    
    
    import macros
    {.experimental: "forLoopMacros".}
    
    macro enumerate*(x: ForLoopStmt): untyped =
      expectKind x, nnkForStmt
      # we strip off the first for loop variable and use
      # it as an integer counter:
      result = newStmtList()
      result.add newVarStmt(x[0], newLit(0))
      var body = x[^1]
      if body.kind != nnkStmtList:
        body = newTree(nnkStmtList, body)
      body.add newCall(bindSym"inc", x[0])
      var newFor = newTree(nnkForStmt)
      for i in 1..x.len-3:
        newFor.add x[i]
      # transform enumerate(X) to 'X'
      newFor.add x[^2][1]
      newFor.add body
      result.add newFor
    
    
    Run

and usage
    
    
    for a, b in enumerate(items([1, 2, 3])):
      echo a, " ", b
    
    
    Run

I'll have a look at refactoring that for enums (unless someone gives it a go 
first) though I fear it might only work at top level.

Also I'm pretty sure you meant narimiran instead of me ;). 

Reply via email to