It could be helpful to support something like the `contextmanager` decorator in 
Python, which lets the user specify "enter" and "exit" as the code before and 
after `yield`, e.g.

  * 
[https://stackoverflow.com/a/24176022/263998](https://stackoverflow.com/a/24176022/263998)
  * 
[https://docs.python.org/2/library/contextlib.html](https://docs.python.org/2/library/contextlib.html)


    
    
    iterator myfunc(name: string):
      var myfile = open(name)
      yield myfile
      myfile.close()
    
    var myit = myfunc(filename)
    
    with context(myit) as f:
      ...
    

or maybe 
    
    
    withcontext myit as f:
      ...
    

I haven't tried this, but maybe something like: 
    
    
    macro withcontext(args, body: untyped): untyped =
      var varValue = args[1]
      var varName = args[^1]
      ...
      template takeImpl(thisit, name, body) =
        block:
          var private {.genSym.} = thisit
          var name = private()
          try:
            body
          finally:
            private()
            assert private.finished()
      # Generate AST
      getAst(takeImpl(varValue, varName, body))
    

Reply via email to