mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-07 16:23:07 +00:00
Fix error when using an existing name as error variable
This commit is contained in:
parent
90ea780ba9
commit
cfe17ca899
@ -1,12 +1,16 @@
|
|||||||
|
import std/macros
|
||||||
import ./without
|
import ./without
|
||||||
import ./private/binderror
|
import ./private/binderror
|
||||||
|
|
||||||
template without*(condition, errorname, body): untyped =
|
macro without*(condition, errorname, body): untyped =
|
||||||
## Used to place guards that ensure that a Result contains a value.
|
## Used to place guards that ensure that a Result contains a value.
|
||||||
## Exposes error when Result does not contain a value.
|
## Exposes error when Result does not contain a value.
|
||||||
|
|
||||||
|
let errorIdent = ident $errorname
|
||||||
|
|
||||||
|
quote do:
|
||||||
var error: ref CatchableError
|
var error: ref CatchableError
|
||||||
|
|
||||||
without captureBindError(error, condition):
|
without captureBindError(error, `condition`):
|
||||||
template errorname: ref CatchableError = error
|
template `errorIdent`: ref CatchableError = error
|
||||||
body
|
`body`
|
||||||
|
|||||||
@ -281,6 +281,15 @@ suite "result":
|
|||||||
check e2.msg == "error2"
|
check e2.msg == "error2"
|
||||||
check e1.msg == "error1"
|
check e1.msg == "error1"
|
||||||
|
|
||||||
|
test "without statement works in generic code using existing error name":
|
||||||
|
let existingName {.used.} = "some variable"
|
||||||
|
|
||||||
|
proc shouldCompile(_: type int): ?!int =
|
||||||
|
without _ =? int.failure "error", existingName:
|
||||||
|
return success 42
|
||||||
|
|
||||||
|
discard int.shouldCompile()
|
||||||
|
|
||||||
test "without statements with error work in nested calls":
|
test "without statements with error work in nested calls":
|
||||||
proc bar(): ?!int =
|
proc bar(): ?!int =
|
||||||
without _ =? int.failure "error", err:
|
without _ =? int.failure "error", err:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user