Skip to content

Commit

Permalink
Merge pull request nim-lang#9895 from GULPF/unpack-defect
Browse files Browse the repository at this point in the history
Make options.UnpackError inherit from system.Defect
  • Loading branch information
Araq committed Dec 8, 2018
2 parents 160a034 + 0a749f1 commit 32a08d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Expand Up @@ -29,6 +29,8 @@

- `osproc.execProcess` now also takes a `workingDir` parameter.

- `options.UnpackError` is no longer a ref type and inherits from `System.Defect` instead of `System.ValueError`.

#### Breaking changes in the compiler

- The compiler now implements the "generic symbol prepass" for `when` statements
Expand Down
34 changes: 15 additions & 19 deletions lib/pure/options.nim
Expand Up @@ -39,17 +39,18 @@
##
## .. code-block:: nim
##
## try:
## assert("abc".find('c').get() == 2) # Immediately extract the value
## except UnpackError: # If there is no value
## assert false # This will not be reached, because the value is present
##
## let found = "abc".find('c')
## assert found.isSome and found.get() == 2
##
## The ``get`` operation demonstrated above returns the underlying value, or
## 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
## 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
## simply never use ``unsafeGet``.
## raises ``UnpackError`` if there is no value. Note that ``UnpackError`` inherits
## from ``system.Defect``, and should therefore never be catched. Instead, rely on
## checking if the option contains a value with ``isSome`` and ``isNone``.
##
## There is another option for 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 you do not care about the tiny overhead that ``get``
## causes, you should simply never use ``unsafeGet``.
##
## How to deal with an absence of a value:
##
Expand All @@ -61,12 +62,7 @@
## assert(result == none(int))
## # It has no value:
## assert(result.isNone)
##
## try:
## echo result.get()
## assert(false) # This will not be reached
## except UnpackError: # Because an exception is raised
## discard

import typetraits

type
Expand All @@ -81,7 +77,7 @@ type
val: T
has: bool

UnpackError* = ref object of ValueError
UnpackError* = object of Defect

proc some*[T](val: T): Option[T] =
## Returns a ``Option`` that has this value.
Expand Down Expand Up @@ -129,7 +125,7 @@ proc get*[T](self: Option[T]): T =
## Returns contents of the Option. If it is none, then an exception is
## thrown.
if self.isNone:
raise UnpackError(msg: "Can't obtain a value from a `none`")
raise newException(UnpackError, "Can't obtain a value from a `none`")
self.val

proc get*[T](self: Option[T], otherwise: T): T =
Expand All @@ -143,7 +139,7 @@ proc get*[T](self: var Option[T]): var T =
## Returns contents of the Option. If it is none, then an exception is
## thrown.
if self.isNone:
raise UnpackError(msg: "Can't obtain a value from a `none`")
raise newException(UnpackError, "Can't obtain a value from a `none`")
return self.val

proc map*[T](self: Option[T], callback: proc (input: T)) =
Expand Down

0 comments on commit 32a08d4

Please sign in to comment.