-
-
Notifications
You must be signed in to change notification settings - Fork 251
Syntax lookup: Polyvar #318
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
Merged
ryyppy
merged 6 commits into
rescript-lang:master
from
kevanstannard:syntax-lookup-polyvar
May 24, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16ef023
Syntak lookup: Polyvar
kevanstannard e5f2089
Update misc_docs/syntax/language_polyvar.mdx
kevanstannard 4e94cb9
Update misc_docs/syntax/language_polyvar.mdx
kevanstannard 685427a
Update misc_docs/syntax/language_polyvar.mdx
ryyppy 137c50b
Update misc_docs/syntax/language_polyvar.mdx
ryyppy ca37f1e
Update misc_docs/syntax/language_polyvar.mdx
ryyppy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.