import macros macro returnsBool(expr: typed): untyped = result = newLit(expr.getType.typeKind == ntyBool) echo expr.repr, (if result.intVal == 1: " does " else: " doesn't "), "return" template guard(cond: untyped): untyped = when returnsBool(cond): if not cond: return else: try: cond except: return template guard(cond, body: untyped): untyped = when returnsBool(cond): if not cond: body return else: try: cond except: body return import strutils proc test(x: int, y: string): int = guard x > 5 guard y.len > 0 result = guard y.strip.parseInt echo result guard x < 100: echo "X is too large!" guard result < 255: reset result block complex: echo "Entering complex logic zone" result *= 5 guard x == y.len: break complex result = result div 2 echo test(10, " 250")