Skip to content

Commit 05711d9

Browse files
authored
Add example for pattern matching on options (#17120)
* Add example for pattern matching on options * Use code-block * Apply suggestions * Use block comments * Fix example
1 parent ef53031 commit 05711d9

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

lib/pure/options.nim

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
# distribution, for details about the copyright.
88
#
99

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+
2628
runnableExamples:
2729
proc find(haystack: string, needle: char): Option[int] =
2830
for i, c in haystack:
@@ -34,11 +36,37 @@ runnableExamples:
3436
let found = "abc".find('c')
3537
assert found.isSome and found.get() == 2
3638

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`
4270

4371

4472
import std/typetraits

0 commit comments

Comments
 (0)