id | keywords | name | summary | category | |||
---|---|---|---|---|---|---|---|
polyvar |
|
#value |
This is the `polymorphic variant` type declaration. |
languageconstructs |
Polymorphic variants (or poly variants) are the structurally typed equivalent of variants.
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.
<CodeTab labels={["ReScript", "JS Output"]}>
type status = [#Waiting | #Running | #Error(string)]
let status1: status = #Waiting
let status2: status = #Error("Network error")
var status1 = "Waiting";
var status2 = {
NAME: "Error",
VAL: "Network error",
};