mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-08 00:33:07 +00:00
Fix: nested without calls in generic code
This commit is contained in:
parent
f6363e19d3
commit
fed12cf7f7
@ -1,15 +1,15 @@
|
|||||||
import std/options
|
import std/options
|
||||||
|
|
||||||
var captureEnabled {.global, compileTime.}: bool
|
var captures {.global, compileTime.}: int
|
||||||
var errorVariable: ptr ref CatchableError
|
var errorVariable: ptr ref CatchableError
|
||||||
|
|
||||||
template captureBindError*(error: var ref CatchableError, expression): auto =
|
template captureBindError*(error: var ref CatchableError, expression): auto =
|
||||||
let previousErrorVariable = errorVariable
|
let previousErrorVariable = errorVariable
|
||||||
errorVariable = addr error
|
errorVariable = addr error
|
||||||
|
|
||||||
static: captureEnabled = true
|
static: inc captures
|
||||||
let evaluated = expression
|
let evaluated = expression
|
||||||
static: captureEnabled = false
|
static: dec captures
|
||||||
|
|
||||||
errorVariable = previousErrorVariable
|
errorVariable = previousErrorVariable
|
||||||
|
|
||||||
@ -19,6 +19,6 @@ func error[T](option: Option[T]): ref CatchableError =
|
|||||||
newException(ValueError, "Option is set to `none`")
|
newException(ValueError, "Option is set to `none`")
|
||||||
|
|
||||||
template bindFailed*(expression) =
|
template bindFailed*(expression) =
|
||||||
when captureEnabled:
|
when captures > 0:
|
||||||
mixin error
|
mixin error
|
||||||
errorVariable[] = expression.error
|
errorVariable[] = expression.error
|
||||||
|
|||||||
@ -294,6 +294,22 @@ suite "result":
|
|||||||
|
|
||||||
foo()
|
foo()
|
||||||
|
|
||||||
|
test "without statement with error works in nested generic calls":
|
||||||
|
proc works(_: type int): ?!int =
|
||||||
|
without _ =? int.failure "error1", err:
|
||||||
|
return success 42
|
||||||
|
|
||||||
|
proc fails(_: type int): ?!int =
|
||||||
|
return failure "error2"
|
||||||
|
|
||||||
|
proc foo =
|
||||||
|
without a =? int.works() and b =? int.fails(), error:
|
||||||
|
check error.msg == "error2"
|
||||||
|
return
|
||||||
|
fail()
|
||||||
|
|
||||||
|
foo()
|
||||||
|
|
||||||
test "catch can be used to convert exceptions to results":
|
test "catch can be used to convert exceptions to results":
|
||||||
check parseInt("42").catch == 42.success
|
check parseInt("42").catch == 42.success
|
||||||
check parseInt("foo").catch.error of ValueError
|
check parseInt("foo").catch.error of ValueError
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user