> Is "withAs" (and context managers) an existing well-known Nim pattern?

Yes, and you can take it further by providing template-only operations: 
    
    
    template withNumber(i: var int, body: untyped) =
      template add(b: int) {.used.} = i += b
      template sub(b: int) {.used.} = i -= b
      body
    
    var n = 5
    withNumber n:
      add(5)
      sub(1)
    
    withNumber n, sub(1)
    withNumber(n, add(1))
    withNumber(n, (sub(2); sub(3);))
    

Reply via email to