7
7
# distribution, for details about the copyright.
8
8
#
9
9
10
- # # This module implements types which encapsulate an optional value.
11
- # #
12
- # # A value of type `Option[T]` either contains a value `x` (represented as
13
- # # `some(x)`) or is empty (`none(T)`).
14
- # #
15
- # # This can be useful when you have a value that can be present or not. The
16
- # # absence of a value is often represented by `nil`, but that is not always
17
- # # available, nor is it always a good solution.
18
- # #
19
- # #
20
- # # Basic usage
21
- # # ===========
22
- # #
23
- # # Let's start with an example: a procedure that finds the index of a character
24
- # # in a string.
25
- # #
10
+ ##[
11
+ This module implements types which encapsulate an optional value.
12
+
13
+ A value of type `Option[T]` either contains a value `x` (represented as
14
+ `some(x)`) or is empty (`none(T)`).
15
+
16
+ This can be useful when you have a value that can be present or not. The
17
+ absence of a value is often represented by `nil`, but that is not always
18
+ available, nor is it always a good solution.
19
+
20
+
21
+ Basic usage
22
+ ===========
23
+
24
+ Let's start with an example: a procedure that finds the index of a character
25
+ in a string.
26
+ ]##
27
+
26
28
runnableExamples:
27
29
proc find (haystack: string , needle: char ): Option [int ] =
28
30
for i, c in haystack:
@@ -34,11 +36,37 @@ runnableExamples:
34
36
let found = " abc" .find ('c' )
35
37
assert found.isSome and found.get () == 2
36
38
37
- # # The `get` operation demonstrated above returns the underlying value, or
38
- # # raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
39
- # # inherits from `system.Defect` and should therefore never be caught.
40
- # # Instead, rely on checking if the option contains a value with the
41
- # # `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
39
+ ##[
40
+ The `get` operation demonstrated above returns the underlying value, or
41
+ raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
42
+ inherits from `system.Defect` and should therefore never be caught.
43
+ Instead, rely on checking if the option contains a value with the
44
+ `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
45
+
46
+
47
+ Pattern matching
48
+ ================
49
+
50
+ .. note:: This requires the [fusion](https://github.com/nim-lang/fusion) package.
51
+
52
+ [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html)
53
+ supports pattern matching on `Option`s, with the `Some(<pattern>)` and
54
+ `None()` patterns.
55
+
56
+ .. code-block:: nim
57
+ {.experimental: "caseStmtMacros".}
58
+
59
+ import fusion/matching
60
+
61
+ case some(42)
62
+ of Some(@a):
63
+ assert a == 42
64
+ of None():
65
+ assert false
66
+
67
+ assertMatch(some(some(none(int))), Some(Some(None())))
68
+ ]##
69
+ # xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule`
42
70
43
71
44
72
import std/ typetraits
0 commit comments