From 37b4a067f14935976d59f787ee8f8d6bf93481b0 Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sat, 20 Feb 2021 17:24:14 +0100 Subject: [PATCH 1/5] Add example for pattern matching on options --- lib/pure/options.nim | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/pure/options.nim b/lib/pure/options.nim index 7abfaa93a5cba..e48a27b8f81f2 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -39,6 +39,27 @@ runnableExamples: ## inherits from `system.Defect` and should therefore never be caught. ## Instead, rely on checking if the option contains a value with the ## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs. +## +## +## Pattern matching +## ================ +## +## [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html) +## supports pattern matching on `Option`s, with the `Some()` and +## `None()` patterns. + +runnableExamples: + {.experimental: "caseStmtMacros".} + + import fusion/matching + + case some(42) + of Some(@a): + assert a == 42 + of None(): + assert false + + Some(Some(None())) := some(some(none(int))) import std/typetraits From c198f2b526bf7797d691c8375dfac1e4d69cd9c6 Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sat, 20 Feb 2021 17:53:51 +0100 Subject: [PATCH 2/5] Use code-block --- lib/pure/options.nim | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/pure/options.nim b/lib/pure/options.nim index e48a27b8f81f2..5ed386fbda038 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -44,22 +44,24 @@ runnableExamples: ## Pattern matching ## ================ ## +## **Note:** This requires the [fusion](https://github.com/nim-lang/fusion) package. +## ## [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html) ## supports pattern matching on `Option`s, with the `Some()` and ## `None()` patterns. - -runnableExamples: - {.experimental: "caseStmtMacros".} - - import fusion/matching - - case some(42) - of Some(@a): - assert a == 42 - of None(): - assert false - - Some(Some(None())) := some(some(none(int))) +## +## .. code-block:: nim +## {.experimental: "caseStmtMacros".} +## +## import fusion/matching +## +## case some(42) +## of Some(@a): +## assert a == 42 +## of None(): +## assert false +## +## Some(Some(None())) := some(some(none(int))) import std/typetraits From 75b0954633f5dee51c817096d10f6025ca10478d Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sat, 20 Feb 2021 22:06:47 +0100 Subject: [PATCH 3/5] Apply suggestions --- lib/pure/options.nim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pure/options.nim b/lib/pure/options.nim index 5ed386fbda038..303a71c591896 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -44,7 +44,7 @@ runnableExamples: ## Pattern matching ## ================ ## -## **Note:** This requires the [fusion](https://github.com/nim-lang/fusion) package. +## .. note:: This requires the [fusion](https://github.com/nim-lang/fusion) package. ## ## [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html) ## supports pattern matching on `Option`s, with the `Some()` and @@ -61,7 +61,8 @@ runnableExamples: ## of None(): ## assert false ## -## Some(Some(None())) := some(some(none(int))) +## assertMatch(Some(Some(None())), some(some(none(int)))) +# xxx pending https://github.com/timotheecour/Nim/issues/376 use `whichModule` import std/typetraits From 39b8fe56e6369343c8977598cf1fc4b78e159c4e Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sat, 20 Feb 2021 22:51:27 +0100 Subject: [PATCH 4/5] Use block comments --- lib/pure/options.nim | 94 +++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/lib/pure/options.nim b/lib/pure/options.nim index 303a71c591896..807c1cf094a26 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -7,22 +7,24 @@ # distribution, for details about the copyright. # -## This module implements types which encapsulate an optional value. -## -## A value of type `Option[T]` either contains a value `x` (represented as -## `some(x)`) or is empty (`none(T)`). -## -## This can be useful when you have a value that can be present or not. The -## absence of a value is often represented by `nil`, but that is not always -## available, nor is it always a good solution. -## -## -## Basic usage -## =========== -## -## Let's start with an example: a procedure that finds the index of a character -## in a string. -## +##[ +This module implements types which encapsulate an optional value. + +A value of type `Option[T]` either contains a value `x` (represented as +`some(x)`) or is empty (`none(T)`). + +This can be useful when you have a value that can be present or not. The +absence of a value is often represented by `nil`, but that is not always +available, nor is it always a good solution. + + +Basic usage +=========== + +Let's start with an example: a procedure that finds the index of a character +in a string. +]## + runnableExamples: proc find(haystack: string, needle: char): Option[int] = for i, c in haystack: @@ -34,35 +36,37 @@ runnableExamples: 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 the -## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs. -## -## -## Pattern matching -## ================ -## -## .. note:: This requires the [fusion](https://github.com/nim-lang/fusion) package. -## -## [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html) -## supports pattern matching on `Option`s, with the `Some()` and -## `None()` patterns. -## -## .. code-block:: nim -## {.experimental: "caseStmtMacros".} -## -## import fusion/matching -## -## case some(42) -## of Some(@a): -## assert a == 42 -## of None(): -## assert false -## -## assertMatch(Some(Some(None())), some(some(none(int)))) -# xxx pending https://github.com/timotheecour/Nim/issues/376 use `whichModule` +##[ +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 the +`isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs. + + +Pattern matching +================ + +.. note:: This requires the [fusion](https://github.com/nim-lang/fusion) package. + +[fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html) +supports pattern matching on `Option`s, with the `Some()` and +`None()` patterns. + +.. code-block:: nim + {.experimental: "caseStmtMacros".} + + import fusion/matching + + case some(42) + of Some(@a): + assert a == 42 + of None(): + assert false + + assertMatch(Some(Some(None())), some(some(none(int)))) +]## +# xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule` import std/typetraits From 17d7f919eddff1ed45bd6c8464120cb1fd5d0800 Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sun, 21 Feb 2021 00:39:23 +0100 Subject: [PATCH 5/5] Fix example --- lib/pure/options.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/options.nim b/lib/pure/options.nim index 807c1cf094a26..69feee7dc5283 100644 --- a/lib/pure/options.nim +++ b/lib/pure/options.nim @@ -64,7 +64,7 @@ supports pattern matching on `Option`s, with the `Some()` and of None(): assert false - assertMatch(Some(Some(None())), some(some(none(int)))) + assertMatch(some(some(none(int))), Some(Some(None()))) ]## # xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule`