Reference types are handled by `without` statement with error
This commit is contained in:
parent
43e7deb827
commit
6ef525cfe2
|
@ -21,9 +21,18 @@ macro captureBindError*(error: var ref CatchableError, expression): auto =
|
||||||
# return the evaluated result
|
# return the evaluated result
|
||||||
evaluated
|
evaluated
|
||||||
|
|
||||||
func error[T](option: Option[T]): ref CatchableError =
|
func error[T](_: Option[T]): ref CatchableError =
|
||||||
newException(ValueError, "Option is set to `none`")
|
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) =
|
macro bindFailed*(expression: typed) =
|
||||||
## Called when a binding (=?) fails.
|
## Called when a binding (=?) fails.
|
||||||
## Assigns an error to the error variable (specified in captureBindError())
|
## Assigns an error to the error variable (specified in captureBindError())
|
||||||
|
|
|
@ -326,6 +326,48 @@ suite "result":
|
||||||
test1()
|
test1()
|
||||||
test2()
|
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":
|
test "without statement with error can be used more than once":
|
||||||
proc test =
|
proc test =
|
||||||
without a =? 42.success, error:
|
without a =? 42.success, error:
|
||||||
|
|
Loading…
Reference in New Issue