|
39 | 39 | ## return none(int) # This line is actually optional,
|
40 | 40 | ## # because the default is empty
|
41 | 41 | ##
|
42 |
| -## .. code-block:: nim |
43 |
| -## require "abc".find('c'): |
44 |
| -## some x: assert(x == 2) |
45 |
| -## none: assert false # This will not be reached, because the string |
46 |
| -## # contains 'c' |
47 |
| -## |
48 |
| -## This pattern assures that you only access the value of on option in a safe |
49 |
| -## way. If you want to use a more familiar exception pattern you can use: |
50 |
| -## |
51 |
| -## .. code-block:: nim |
52 |
| -## |
53 | 42 | ## try:
|
54 | 43 | ## assert("abc".find('c').get() == 2) # Immediately extract the value
|
55 | 44 | ## except UnpackError: # If there is no value
|
|
59 | 48 | ## raises ``UnpackError`` if there is no value. There is another option for
|
60 | 49 | ## obtaining the value: ``unsafeGet``, but you must only use it when you are
|
61 | 50 | ## absolutely sure the value is present (e.g. after checking ``isSome``). If
|
62 |
| -## you do not care about the tiny overhead that ``get`` and ``require`` causes, |
63 |
| -## you should simply never use ``unsafeGet``. |
64 |
| -## |
65 |
| -## By far the safest and simplest way to use an option value is the ``require`` |
66 |
| -## procedure. It can take either a single statement as seen above, or multiple. |
67 |
| -## Note that in the same way as the other operators work ``require`` will not |
68 |
| -## evaluate all statements if an earlier one doesn't return a value: |
69 |
| -## |
70 |
| -## .. code-block:: nim |
71 |
| -## let msg = require([some(-100), some(200)]): |
72 |
| -## some [x, _]: "First value is " & $x |
73 |
| -## none: "No value" |
74 |
| -## echo msg # This will echo "First value is -100" |
75 |
| -## |
76 |
| -## As seen above you can ignore the value of the option with an underscore as |
77 |
| -## the identifier. But if you want to assign it to a locally available symbol |
78 |
| -## simply add it in the same position in the list. The underscore can be used to |
79 |
| -## ignore one value, the entire list of values, or only some of the values. |
80 |
| -## |
81 |
| -## This module also implements common mapping functionality to use a procedure |
82 |
| -## on an optional value, carrying over the absence state if there is no value: |
83 |
| -## |
84 |
| -## .. code-block:: nim |
85 |
| -## |
86 |
| -## let |
87 |
| -## x = some(10) |
88 |
| -## y = none(int) |
89 |
| -## assert(map(x, proc(x: int): int = x + 10) == some(20)) |
90 |
| -## assert(map(y, proc(x: int): int = x + 10).isNone) |
91 |
| -## |
92 |
| -## Options can also be used for conditional chaining. Let's use the ``find`` |
93 |
| -## procedure we defined above: |
94 |
| -## |
95 |
| -## .. code-block:: nim |
96 |
| -## var position = "hello world".find('w') |
97 |
| -## echo position # echoes out Some(6) is expected |
98 |
| -## position = "hello world".find('w').?min(4) |
99 |
| -## echo position # echoes out Some(4), we take the minimum of 6 and 4 |
100 |
| -## position = "hello world".find('q').?min(4) |
101 |
| -## echo position # echoes out None[int], min is never run since find returns |
102 |
| -## # a none option. |
103 |
| -## position = "hello world".find('w').?min(4).max(0) |
104 |
| -## echo position # echoes out Some(4) both min and max are run |
105 |
| -## position = "hello world".find('q').?min(4).max(0) |
106 |
| -## echo position # echoes out None[int], min and max is never run |
107 |
| -## |
108 |
| -## Since options use their has-ity as their boolean value this chaining can |
109 |
| -## also be used in if statements: |
110 |
| -## |
111 |
| -## .. code-block:: nim |
112 |
| -## if ("hello world".find('w').?min(4)).isSome: |
113 |
| -## echo "Never run" |
| 51 | +## you do not care about the tiny overhead that ``get`` causes, |
| 52 | +## you should simply never use ``unsafeGet``. Also have a look at ``optionutils`` |
| 53 | +## for more ways of safely interacting with optional values. Prior to 0.19.0 many |
| 54 | +## of the features found in that module was part of this module. This module now |
| 55 | +## only contains the basic concept of options. |
114 | 56 | import typetraits
|
115 | 57 |
|
116 | 58 | type
|
|
0 commit comments