From 16ef023b70881c1b1ac4b0a4bb36a1d448f12f20 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Tue, 4 May 2021 20:20:02 +1000 Subject: [PATCH 1/6] Syntak lookup: Polyvar --- misc_docs/syntax/language_polyvar.mdx | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 misc_docs/syntax/language_polyvar.mdx diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx new file mode 100644 index 000000000..62ffc4304 --- /dev/null +++ b/misc_docs/syntax/language_polyvar.mdx @@ -0,0 +1,39 @@ +--- +id: "polyvar" +keywords: ["polymorphic", "variant", "polyvar"] +name: "[ #value ]" +summary: "This is the `polymorphic variant` type declaration." +category: "languageconstructs" +--- + +[Polymorphic variants](/docs/manual/latest/polymorphic-variant) (or _poly variants_) are a specific kind of [variant](/docs/manual/latest/variant). + +Their usage doesn't require any explicit type definition, and they not coupled to any specific module. The compiler will infer the type on demand, and compare poly variants by their value, instead of their type name. + +However, using the `[ #value ]` syntax you can to define a _closed poly variant type_ with an exact set of values, known as constructors. + +### Example + + + +```res +type status = [#Waiting | #Running | #Error(string)] + +let status1: status = #Waiting +let status2: status = #Error("Network error") +``` + +```js +var status1 = "Waiting"; + +var status2 = { + NAME: "Error", + VAL: "Network error", +}; +``` + + + +### References + +* [Polymorphic Variant](/docs/manual/latest/polymorphic-variant) From e5f2089996577c6c9a3374d2dea57fc2dc7887d7 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Thu, 6 May 2021 07:29:18 +1000 Subject: [PATCH 2/6] Update misc_docs/syntax/language_polyvar.mdx Co-authored-by: Patrick Ecker --- misc_docs/syntax/language_polyvar.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx index 62ffc4304..a26200ef9 100644 --- a/misc_docs/syntax/language_polyvar.mdx +++ b/misc_docs/syntax/language_polyvar.mdx @@ -6,7 +6,7 @@ summary: "This is the `polymorphic variant` type declaration." category: "languageconstructs" --- -[Polymorphic variants](/docs/manual/latest/polymorphic-variant) (or _poly variants_) are a specific kind of [variant](/docs/manual/latest/variant). +[Polymorphic variants](/docs/manual/latest/polymorphic-variant) (or _poly variants_) are the structurally typed equivalent of [variants](/docs/manual/latest/variant). Their usage doesn't require any explicit type definition, and they not coupled to any specific module. The compiler will infer the type on demand, and compare poly variants by their value, instead of their type name. From 4e94cb96b62eb5be9af9a24c828cf9aea169660f Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Thu, 6 May 2021 07:30:03 +1000 Subject: [PATCH 3/6] Update misc_docs/syntax/language_polyvar.mdx Co-authored-by: Patrick Ecker --- misc_docs/syntax/language_polyvar.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx index a26200ef9..8b00dc29c 100644 --- a/misc_docs/syntax/language_polyvar.mdx +++ b/misc_docs/syntax/language_polyvar.mdx @@ -8,7 +8,7 @@ category: "languageconstructs" [Polymorphic variants](/docs/manual/latest/polymorphic-variant) (or _poly variants_) are the structurally typed equivalent of [variants](/docs/manual/latest/variant). -Their usage doesn't require any explicit type definition, and they not coupled to any specific module. The compiler will infer the type on demand, and compare poly variants by their value, instead of their type name. +In comparison to nominally typed variants, their values don't require any explicit type definition, and are not coupled to any specific module. The compiler will infer the type on demand, and compare poly variants by their value, instead of their type name. However, using the `[ #value ]` syntax you can to define a _closed poly variant type_ with an exact set of values, known as constructors. From 685427aabc1e400fc514010e225dd4975fbc4bbc Mon Sep 17 00:00:00 2001 From: Patrick Ecker Date: Mon, 24 May 2021 11:14:35 +0200 Subject: [PATCH 4/6] Update misc_docs/syntax/language_polyvar.mdx --- misc_docs/syntax/language_polyvar.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx index 8b00dc29c..78758a92b 100644 --- a/misc_docs/syntax/language_polyvar.mdx +++ b/misc_docs/syntax/language_polyvar.mdx @@ -1,7 +1,7 @@ --- id: "polyvar" keywords: ["polymorphic", "variant", "polyvar"] -name: "[ #value ]" +name: "#value" summary: "This is the `polymorphic variant` type declaration." category: "languageconstructs" --- From 137c50b3930f37714a6e5fb3156c7801b0c0a792 Mon Sep 17 00:00:00 2001 From: Patrick Ecker Date: Mon, 24 May 2021 11:23:55 +0200 Subject: [PATCH 5/6] Update misc_docs/syntax/language_polyvar.mdx --- misc_docs/syntax/language_polyvar.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx index 78758a92b..90a659dbe 100644 --- a/misc_docs/syntax/language_polyvar.mdx +++ b/misc_docs/syntax/language_polyvar.mdx @@ -10,7 +10,7 @@ category: "languageconstructs" In comparison to nominally typed variants, their values don't require any explicit type definition, and are not coupled to any specific module. The compiler will infer the type on demand, and compare poly variants by their value, instead of their type name. -However, using the `[ #value ]` syntax you can to define a _closed poly variant type_ with an exact set of values, known as constructors. +However, you can also describe a set of polymorphic variants with a type declaration, which can be useful to explicitly annotate function parameters and values. The most common format is the _closed poly variant_ type definition that looks like this: `type x = [ #value ]` (Note the extra `[]` around the poly variant constructors!). ### Example From ca37f1e4d2634c415a306aabb3227f98848ed2b7 Mon Sep 17 00:00:00 2001 From: Patrick Ecker Date: Mon, 24 May 2021 11:24:00 +0200 Subject: [PATCH 6/6] Update misc_docs/syntax/language_polyvar.mdx --- misc_docs/syntax/language_polyvar.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx index 90a659dbe..39fbd68ce 100644 --- a/misc_docs/syntax/language_polyvar.mdx +++ b/misc_docs/syntax/language_polyvar.mdx @@ -2,7 +2,7 @@ id: "polyvar" keywords: ["polymorphic", "variant", "polyvar"] name: "#value" -summary: "This is the `polymorphic variant` type declaration." +summary: "This is a `polymorphic variant`." category: "languageconstructs" ---