diff --git a/stew/results.nim b/stew/results.nim index cd24d2d..f8996cd 100644 --- a/stew/results.nim +++ b/stew/results.nim @@ -731,7 +731,11 @@ template catch*(body: typed): Result[type(body), ref CatchableError] = type R = Result[type(body), ref CatchableError] try: - R.ok(body) + when type(body) is void: + body + R.ok() + else: + R.ok(body) except CatchableError as eResultPrivate: R.err(eResultPrivate) diff --git a/tests/test_results.nim b/tests/test_results.nim index 3e894c2..2b12fd2 100644 --- a/tests/test_results.nim +++ b/tests/test_results.nim @@ -89,12 +89,21 @@ block: doAssert rOkV == $rOk.get() # Exceptions -> results - func raises(): int = - raise (ref CatchableError)(msg: "hello") + block: + func raises(): int = + raise (ref CatchableError)(msg: "hello") + func raisesVoid() = + raise (ref CatchableError)(msg: "hello") - let c = catch: - raises() - doAssert c.isErr + let c = catch: + raises() + doAssert c.isErr + + when (NimMajor, NimMinor) >= (1, 6): + # Earlier versions complain about the type of the raisesVoid expression + let d = catch: + raisesVoid() + doAssert d.isErr # De-reference when (NimMajor, NimMinor) >= (1, 6):