Skip to content

Instantly share code, notes, and snippets.

@Araq
Created September 2, 2020 15:32
Show Gist options
  • Save Araq/35dbc6bc70b15f6e256f0493f05ef899 to your computer and use it in GitHub Desktop.
Save Araq/35dbc6bc70b15f6e256f0493f05ef899 to your computer and use it in GitHub Desktop.
import macros
macro awaitE(call: typed, r: untyped): untyped =
let x = call[0]
let tx = x.getType()
expectKind tx, nnkBracketExpr
var t = newTree(nnkProcTy)
var p = newTree(nnkFormalParams, tx[1])
for i in 2 ..< tx.len:
p.add newIdentDefs(ident("param" & $(i-1)), tx[i], newEmptyNode())
t.add p
t.add newTree(nnkPragma, ident("nimcall"), newTree(nnkExprColonExpr, ident"raises", r))
echo repr t
template check(t, x) = discard [t(x)]
result = newTree(nnkStmtListExpr, getAst(check(t, x)), call)
proc baz(x: int) = raise newException(IOError, "test")
awaitE baz(3), [OSError]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment