From f02b7ff9af979c11cf040e4c2f466b7d9dc684cb Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Tue, 4 May 2021 20:46:24 +1000 Subject: [PATCH 1/3] Syntax lookup: Switch --- misc_docs/syntax/language_switch.mdx | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 misc_docs/syntax/language_switch.mdx diff --git a/misc_docs/syntax/language_switch.mdx b/misc_docs/syntax/language_switch.mdx new file mode 100644 index 000000000..126a4f954 --- /dev/null +++ b/misc_docs/syntax/language_switch.mdx @@ -0,0 +1,46 @@ +--- +id: "switch" +keywords: ["switch", "pattern", "match"] +name: "switch" +summary: "This is the `switch` pattern matching keyword." +category: "languageconstructs" +--- + +A `switch` expression allows you to execute some code based on the shape of the data. + +### Example + + + +```res +type shape = Circle(float) | Square(float) + +let shape = Square(3.0) + +let message = switch shape { +| Circle(radius) => "Circle with radius " ++ Js.Float.toString(radius) +| Square(length) => "Square with sides of length " ++ Js.Float.toString(length) +} +``` + +```js +var shape = { + TAG: /* Square */ 1, + _0: 3.0, +}; + +var message; + +message = + shape.TAG === /* Circle */ 0 + ? "Circle with radius " + (3.0).toString() + : "Square with sides of length " + (3.0).toString(); +``` + + + + +### References + +* [Switch Based on Shape of Data](/docs/manual/latest/pattern-matching-destructuring#switch-based-on-shape-of-data) + From ad2b18118a040e8c2ac94a7604cfa9b79717673d Mon Sep 17 00:00:00 2001 From: Patrick Ecker Date: Wed, 5 May 2021 21:18:28 +0200 Subject: [PATCH 2/3] Update misc_docs/syntax/language_switch.mdx --- misc_docs/syntax/language_switch.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/misc_docs/syntax/language_switch.mdx b/misc_docs/syntax/language_switch.mdx index 126a4f954..eab8766e2 100644 --- a/misc_docs/syntax/language_switch.mdx +++ b/misc_docs/syntax/language_switch.mdx @@ -6,7 +6,7 @@ summary: "This is the `switch` pattern matching keyword." category: "languageconstructs" --- -A `switch` expression allows you to execute some code based on the shape of the data. +A `switch` expression allows you to match / destructure almost any kind of value (array, list, records, strings, numbers, variants, etc). Each branch (pattern) of a `switch` expression must return a value of the same type. ### Example @@ -43,4 +43,3 @@ message = ### References * [Switch Based on Shape of Data](/docs/manual/latest/pattern-matching-destructuring#switch-based-on-shape-of-data) - From a389689cb8525ac759ab9e84c25e749eeb83ae04 Mon Sep 17 00:00:00 2001 From: Patrick Ecker Date: Wed, 5 May 2021 21:24:16 +0200 Subject: [PATCH 3/3] Update misc_docs/syntax/language_switch.mdx --- misc_docs/syntax/language_switch.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_switch.mdx b/misc_docs/syntax/language_switch.mdx index eab8766e2..f04d74c21 100644 --- a/misc_docs/syntax/language_switch.mdx +++ b/misc_docs/syntax/language_switch.mdx @@ -2,7 +2,7 @@ id: "switch" keywords: ["switch", "pattern", "match"] name: "switch" -summary: "This is the `switch` pattern matching keyword." +summary: "This is the `switch` expression keyword." category: "languageconstructs" ---