Skip to content

Commit

Permalink
Reference types are handled by without statement with error
Browse files Browse the repository at this point in the history
  • Loading branch information
markspanbroek committed Jan 9, 2024
1 parent 43e7deb commit bdaec76
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion questionable/private/binderror.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ macro captureBindError*(error: var ref CatchableError, expression): auto =
# return the evaluated result
evaluated

func error[T](option: Option[T]): ref CatchableError =
func error[T](_: Option[T]): ref CatchableError =
newException(ValueError, "Option is set to `none`")

func error[T](_: ref T): ref CatchableError =
newException(ValueError, "ref is nil")

func error[T](_: ptr T): ref CatchableError =
newException(ValueError, "ptr is nil")

func error[Proc: proc | iterator](_: Proc): ref CatchableError =
newException(ValueError, "proc or iterator is nil")

macro bindFailed*(expression: typed) =
## Called when a binding (=?) fails.
## Assigns an error to the error variable (specified in captureBindError())
Expand Down
42 changes: 42 additions & 0 deletions testmodules/results/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,48 @@ suite "result":
test1()
test2()

test "without statement with error handles references as well":
proc test =
var x: ref int = nil
without a =? x, error:
check error.msg == "ref is nil"
return
fail

test()

test "without statement with error handles pointers as well":
proc test =
var x: ptr int = nil
without a =? x, error:
check error.msg == "ptr is nil"
return
fail

test()

test "without statement with error handles closures as well":
proc test =
var x = proc = discard
x = nil
without a =? x, error:
check error.msg == "proc or iterator is nil"
return
fail

test()

test "without statement with error handles iterators as well":
when (NimMajor, NimMinor) != (2, 0):
proc test =
var x: iterator: int = nil
without a =? x, error:
check error.msg == "proc or iterator is nil"
return
fail

test()

test "without statement with error can be used more than once":
proc test =
without a =? 42.success, error:
Expand Down

0 comments on commit bdaec76

Please sign in to comment.