mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-23 10:29:44 +00:00
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]
|
type R = Result[type(body), ref CatchableError]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
R.ok(body)
|
when type(body) is void:
|
||||||
|
body
|
||||||
|
R.ok()
|
||||||
|
else:
|
||||||
|
R.ok(body)
|
||||||
except CatchableError as eResultPrivate:
|
except CatchableError as eResultPrivate:
|
||||||
R.err(eResultPrivate)
|
R.err(eResultPrivate)
|
||||||
|
|
||||||
|
@ -89,12 +89,21 @@ block:
|
|||||||
doAssert rOkV == $rOk.get()
|
doAssert rOkV == $rOk.get()
|
||||||
|
|
||||||
# Exceptions -> results
|
# Exceptions -> results
|
||||||
func raises(): int =
|
block:
|
||||||
raise (ref CatchableError)(msg: "hello")
|
func raises(): int =
|
||||||
|
raise (ref CatchableError)(msg: "hello")
|
||||||
|
func raisesVoid() =
|
||||||
|
raise (ref CatchableError)(msg: "hello")
|
||||||
|
|
||||||
let c = catch:
|
let c = catch:
|
||||||
raises()
|
raises()
|
||||||
doAssert c.isErr
|
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
|
# De-reference
|
||||||
when (NimMajor, NimMinor) >= (1, 6):
|
when (NimMajor, NimMinor) >= (1, 6):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user