diff options
author | flywind <43030857+xflywind@users.noreply.github.com> | 2020-12-30 09:26:49 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-30 16:26:49 +0100 |
commit | 805917768dea1b223a3aab3131d5c76f862b4984 (patch) | |
tree | a591638fd906fc10f4e8bb076fc26addfb3edac9 | |
parent | b8658a3e242f7304f7b0320089ef5aa0d5375c25 (diff) | |
download | Nim-805917768dea1b223a3aab3131d5c76f862b4984.tar.gz |
use runnableExamples in options (#16503)
-rw-r--r-- | lib/pure/options.nim | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/lib/pure/options.nim b/lib/pure/options.nim index 8de4430c1..195da1008 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -23,38 +23,29 @@ ## Let's start with an example: a procedure that finds the index of a character ## in a string. ## -## .. code-block:: nim -## -## import options -## -## proc find(haystack: string, needle: char): Option[int] = -## for i, c in haystack: -## if c == needle: -## return some(i) -## return none(int) # This line is actually optional, -## # because the default is empty -## -## .. code-block:: nim -## -## let found = "abc".find('c') -## assert found.isSome and found.get() == 2 -## +runnableExamples: + proc find(haystack: string, needle: char): Option[int] = + for i, c in haystack: + if c == needle: + return some(i) + return none(int) # This line is actually optional, + # because the default is empty + + let found = "abc".find('c') + assert found.isSome and found.get() == 2 + ## The `get` operation demonstrated above returns the underlying value, or ## raises `UnpackDefect` if there is no value. Note that `UnpackDefect` ## inherits from `system.Defect`, and should therefore never be caught. ## Instead, rely on checking if the option contains a value with ## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs. -## +## ## How to deal with an absence of a value: -## -## .. code-block:: nim -## -## let result = "team".find('i') -## -## # Nothing was found, so the result is `none`. -## assert(result == none(int)) -## # It has no value: -## assert(result.isNone) + +runnableExamples: + let result = none(int) + # It has no value: + assert(result.isNone) import typetraits |