diff --git a/misc_docs/syntax/language_polyvar.mdx b/misc_docs/syntax/language_polyvar.mdx new file mode 100644 index 000000000..39fbd68ce --- /dev/null +++ b/misc_docs/syntax/language_polyvar.mdx @@ -0,0 +1,39 @@ +--- +id: "polyvar" +keywords: ["polymorphic", "variant", "polyvar"] +name: "#value" +summary: "This is a `polymorphic variant`." +category: "languageconstructs" +--- + +[Polymorphic variants](/docs/manual/latest/polymorphic-variant) (or _poly variants_) are the structurally typed equivalent of [variants](/docs/manual/latest/variant). + +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, 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 + + + +```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)