Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax lookup: Polyvar #318

Merged
merged 6 commits into from
May 24, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions misc_docs/syntax/language_polyvar.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
id: "polyvar"
keywords: ["polymorphic", "variant", "polyvar"]
name: "[ #value ]"
ryyppy marked this conversation as resolved.
Show resolved Hide resolved
summary: "This is the `polymorphic variant` type declaration."
ryyppy marked this conversation as resolved.
Show resolved Hide resolved
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, using the `[ #value ]` syntax you can to define a _closed poly variant type_ with an exact set of values, known as constructors.
ryyppy marked this conversation as resolved.
Show resolved Hide resolved

### Example

<CodeTab labels={["ReScript", "JS Output"]}>

```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",
};
```

</CodeTab>

### References

* [Polymorphic Variant](/docs/manual/latest/polymorphic-variant)