-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add section about extensible variant types. #211
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 15 commits into
rescript-lang:master
from
fhammerschmidt:extensible-variants
Sep 8, 2021
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
98ac195
Add section about extensible variant types.
fhammerschmidt 55f3cfd
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 69d88ff
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 41a9005
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 2b06bcc
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy b8291e2
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 8312fff
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy edecfe9
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy a923f1a
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy f2aa4f4
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 390ae1b
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy dc59d5a
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy e116ae0
Update pages/docs/manual/latest/extensible-variant.mdx
ryyppy 36d7ff0
Update data/sidebar_manual_latest.json
ryyppy 878e105
Update pages/docs/manual/latest/extensible-variant.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
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,73 @@ | ||
--- | ||
title: "Extensible Variant" | ||
description: "Extensible Variants in ReScript" | ||
canonical: "/docs/manual/latest/extensible-variant" | ||
--- | ||
|
||
# Extensible Variant | ||
|
||
Variant types are usually constrained to a fixed set of constructors. There may be very rare cases where you still want to be able to add constructors to a variant type even after its initial type declaration. For this, we offer extensible variant types. | ||
|
||
## Definition and Usage | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res example | ||
type t = .. | ||
|
||
type t += Other | ||
|
||
type t += | ||
| Point(float, float) | ||
| Line(float, float, float, float) | ||
``` | ||
```js | ||
var Caml_exceptions = require("./stdlib/caml_exceptions.js"); | ||
|
||
var Other = Caml_exceptions.create("Playground.Other"); | ||
|
||
var Point = Caml_exceptions.create("Playground.Point"); | ||
|
||
var Line = Caml_exceptions.create("Playground.Line"); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
The `..` in the type declaration above defines an extensible variant `type t`. The `+=` operator is then used to add constructors to the given type. | ||
ryyppy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Pattern Matching Caveats | ||
|
||
Extensible variants are open-ended, so the compiler will not be able to exhaustively pattern match all available cases. You will always need to provide a default `_` case for every `switch` expression. | ||
|
||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
|
||
```res | ||
let print = v => | ||
switch v { | ||
| Point(x, y) => Js.log2("Point", (x, y)) | ||
| Line(ax, ay, bx, by) => Js.log2("Line", (ax, ay, bx, by)) | ||
| Other | ||
| _ => Js.log("Other") | ||
} | ||
``` | ||
```js | ||
function print(v) { | ||
if (v.RE_EXN_ID === Point) { | ||
console.log("Point", [v._1, v._2]); | ||
} else if (v.RE_EXN_ID === Line) { | ||
console.log("Line", [v._1, v._2, v._3, v._4]); | ||
} else { | ||
console.log("Other"); | ||
} | ||
} | ||
``` | ||
|
||
</CodeTab> | ||
|
||
## Tips & Tricks | ||
|
||
**Fun fact:** In ReScript, [exceptions](./exception) are actually extensible variants under the hood, so `exception UserError(string)` is equivalent to `type exn += UserError(string)`. It's one of the very few use-case where extensible variants make sense. | ||
|
||
We usually recommend sticking with common [variants](./variant) as much as possible to reap the benefits of exhaustive pattern matching. |
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.