diff --git a/questionable/withoutresult.nim b/questionable/withoutresult.nim index 7872a80..a34ccff 100644 --- a/questionable/withoutresult.nim +++ b/questionable/withoutresult.nim @@ -1,12 +1,16 @@ +import std/macros import ./without 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. ## Exposes error when Result does not contain a value. - var error: ref CatchableError + let errorIdent = ident $errorname - without captureBindError(error, condition): - template errorname: ref CatchableError = error - body + quote do: + var error: ref CatchableError + + without captureBindError(error, `condition`): + template `errorIdent`: ref CatchableError = error + `body` diff --git a/testmodules/result/test.nim b/testmodules/result/test.nim index 21b9bfc..b1bff65 100644 --- a/testmodules/result/test.nim +++ b/testmodules/result/test.nim @@ -281,6 +281,15 @@ suite "result": check e2.msg == "error2" 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": proc bar(): ?!int = without _ =? int.failure "error", err: