results: fix `catch` template for statements (#201)

* results: fix `catch` template for statements

* disable void catch tests on pre-1.6
This commit is contained in:
Jacek Sieka 2023-06-28 10:57:00 +02:00 committed by GitHub
parent d085e48e89
commit 8bb07fac39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -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)

View File

@ -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):