Created
September 2, 2020 15:32
-
-
Save Araq/35dbc6bc70b15f6e256f0493f05ef899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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