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:
parent
d085e48e89
commit
8bb07fac39
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue