Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference types are handled by without statement with error #56

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading