From 5c06bed6fc47327b8f47012fa40317afebe7e672 Mon Sep 17 00:00:00 2001 From: cPlayIt Date: Sat, 19 Jun 2021 15:24:17 -0400 Subject: [PATCH 1/3] reworded and clarified example code --- doc/modules/language-guide/pages/pattern-matching.adoc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/modules/language-guide/pages/pattern-matching.adoc b/doc/modules/language-guide/pages/pattern-matching.adoc index e89a8a7decb..8815b167b3f 100644 --- a/doc/modules/language-guide/pages/pattern-matching.adoc +++ b/doc/modules/language-guide/pages/pattern-matching.adoc @@ -23,13 +23,14 @@ func fullName({ first : Text; mid : Text; last : Text }) : Text { The input is an (anonymous) object, which is deconstructed into its three `Text` fields, whose values are bound to the identifiers `first`, `mid` and `last`. They can be freely used in the block that forms the body of the function. Above we have resorted to _name punning_ for object field patterns, their general form allows the binding identifier to be distinct from the field name, as in `mid = middle_name : Text`. -You can also use pattern matching to search for _literal patterns_, which look just like literal constants. Literal patterns are especially useful in `switch` expressions because they can cause the current pattern match to _fail_, and thus start to match the next pattern. For example: +You can also use pattern matching to declare _literal patterns_, which look just like literal constants. Literal patterns are especially useful in `switch` expressions because they can cause the current pattern match to _fail_, and thus start to match the next pattern. For example: [source, motoko] .... switch ("Adrienne", #female) { - case (name, #female) { "It's a girl! " # name }; - case (name, _) { name # ", a boy!" }; + case (name, #female) { name # " is a girl!" }; + case (name, #male) { name # " is a boy!" }; + case (name, _) { name # ", is a human!" }; } .... From ddd465036463180ab4067bbe470d2d1ca37ec73a Mon Sep 17 00:00:00 2001 From: cPlayIt Date: Sat, 19 Jun 2021 15:32:44 -0400 Subject: [PATCH 2/3] clarified example code and description. --- doc/modules/language-guide/pages/pattern-matching.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/modules/language-guide/pages/pattern-matching.adoc b/doc/modules/language-guide/pages/pattern-matching.adoc index 8815b167b3f..62cbac0ffac 100644 --- a/doc/modules/language-guide/pages/pattern-matching.adoc +++ b/doc/modules/language-guide/pages/pattern-matching.adoc @@ -34,7 +34,7 @@ switch ("Adrienne", #female) { } .... -will match the first `case` clause (because binding to the identifier `name` cannot fail and the shorthand variant literal `#Female` compares as equal), and evaluate to `"It's a girl! Adrienne"`. The second clause showcases the _wildcard_ pattern `_`. It cannot fail, but won't bind any identifier. +will match the first `case` clause (because binding to the identifier `name` cannot fail and the shorthand variant literal `#Female` compares as equal), and evaluate to `"Adrienne is a girl!"`. The last clause showcases the _wildcard_ pattern `_`. It cannot fail, but won't bind any identifier. The last kind of pattern is the `or`-pattern. As their name suggests, these are two or more patterns that are separated by the keyword `or`. Each of the sub-patterns must bind to the same set of identifiers, and is matched from left-to-right. An `or`-pattern fails when its rightmost sub-pattern fails. From fe86405fa1083f19176f0fa8695e431826599c04 Mon Sep 17 00:00:00 2001 From: cPlayIt Date: Sat, 19 Jun 2021 16:01:58 -0400 Subject: [PATCH 3/3] reworded --- doc/modules/language-guide/pages/pattern-matching.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/modules/language-guide/pages/pattern-matching.adoc b/doc/modules/language-guide/pages/pattern-matching.adoc index 62cbac0ffac..238d5c39337 100644 --- a/doc/modules/language-guide/pages/pattern-matching.adoc +++ b/doc/modules/language-guide/pages/pattern-matching.adoc @@ -36,11 +36,11 @@ switch ("Adrienne", #female) { will match the first `case` clause (because binding to the identifier `name` cannot fail and the shorthand variant literal `#Female` compares as equal), and evaluate to `"Adrienne is a girl!"`. The last clause showcases the _wildcard_ pattern `_`. It cannot fail, but won't bind any identifier. -The last kind of pattern is the `or`-pattern. As their name suggests, these are two or more patterns that are separated by the keyword `or`. Each of the sub-patterns must bind to the same set of identifiers, and is matched from left-to-right. An `or`-pattern fails when its rightmost sub-pattern fails. +The last type of pattern is the `or` pattern. As their name suggests, these are two or more patterns that are separated by the keyword `or`. Each of the sub-patterns must bind to the same set of identifiers, and is matched from left-to-right. An `or` pattern fails when its rightmost sub-pattern fails. .The following table summarises the different ways of pattern matching. |=== -|pattern kind |examples |appears in |can fail |remarks +|pattern type |example(s) |context |can fail |remarks |literal |`null`, `42`, `()`, `"Hi"`