Use custom exception for option unpack
This commit is contained in:
parent
ae0c8573f2
commit
caa730127b
1 changed files with 6 additions and 5 deletions
|
|
@ -43,11 +43,11 @@
|
||||||
##
|
##
|
||||||
## try:
|
## try:
|
||||||
## assert("abc".find('c').get() == 2) # Immediately extract the value
|
## assert("abc".find('c').get() == 2) # Immediately extract the value
|
||||||
## except FieldError: # If there is no value
|
## except UnpackError: # If there is no value
|
||||||
## assert false # This will not be reached, because the value is present
|
## assert false # This will not be reached, because the value is present
|
||||||
##
|
##
|
||||||
## The ``get`` operation demonstrated above returns the underlying value, or
|
## The ``get`` operation demonstrated above returns the underlying value, or
|
||||||
## raises ``FieldError`` if there is no value. There is another option for
|
## raises ``UnpackError`` if there is no value. There is another option for
|
||||||
## obtaining the value: ``unsafeGet``, but you must only use it when you are
|
## obtaining the value: ``unsafeGet``, but you must only use it when you are
|
||||||
## absolutely sure the value is present (e.g. after checking ``isSome``). If
|
## absolutely sure the value is present (e.g. after checking ``isSome``). If
|
||||||
## you do not care about the tiny overhead that ``get`` causes, you should
|
## you do not care about the tiny overhead that ``get`` causes, you should
|
||||||
|
|
@ -67,7 +67,7 @@
|
||||||
## try:
|
## try:
|
||||||
## echo result.get()
|
## echo result.get()
|
||||||
## assert(false) # This will not be reached
|
## assert(false) # This will not be reached
|
||||||
## except FieldError: # Because an exception is raised
|
## except UnpackError: # Because an exception is raised
|
||||||
## discard
|
## discard
|
||||||
|
|
||||||
import typetraits
|
import typetraits
|
||||||
|
|
@ -78,6 +78,7 @@ type
|
||||||
## An optional type that stores its value and state separately in a boolean.
|
## An optional type that stores its value and state separately in a boolean.
|
||||||
val: T
|
val: T
|
||||||
has: bool
|
has: bool
|
||||||
|
UnpackError* = ref object of ValueError
|
||||||
|
|
||||||
|
|
||||||
proc some*[T](val: T): Option[T] =
|
proc some*[T](val: T): Option[T] =
|
||||||
|
|
@ -106,7 +107,7 @@ proc get*[T](self: Option[T]): T =
|
||||||
## Returns contents of the Option. If it is none, then an exception is
|
## Returns contents of the Option. If it is none, then an exception is
|
||||||
## thrown.
|
## thrown.
|
||||||
if self.isNone:
|
if self.isNone:
|
||||||
raise newException(FieldError, "Can't obtain a value from a `none`")
|
raise UnpackError(msg : "Can't obtain a value from a `none`")
|
||||||
self.val
|
self.val
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -144,7 +145,7 @@ when isMainModule:
|
||||||
check some("a").isSome
|
check some("a").isSome
|
||||||
|
|
||||||
test "none":
|
test "none":
|
||||||
expect FieldError:
|
expect UnpackError:
|
||||||
discard none(int).get()
|
discard none(int).get()
|
||||||
check(none(int).isNone)
|
check(none(int).isNone)
|
||||||
check(not none(string).isSome)
|
check(not none(string).isSome)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue