Skip to content

Commit b1479c4

Browse files
Syntax lookup type coercion operator (#435)
1 parent 048ce1f commit b1479c4

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: "type-coercion"
3+
keywords: ["operator", "type", "coercion", "polymorphic", "variant", "polyvar", "object"]
4+
name: ":>"
5+
summary: "This is the `type coercion` operator."
6+
category: "operators"
7+
---
8+
9+
The `:>` operator may be used to convert a polymorphic variant to a `string` or `int`, or convert an [object](/docs/manual/latest/object) to a type with a subset of its fields.
10+
11+
### Example 1
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
type color = [#Red | #Green | #Blue]
17+
let color: color = #Red
18+
let message = "The color is " ++ (color :> string)
19+
```
20+
21+
```js
22+
var message = "The color is Red";
23+
```
24+
25+
</CodeTab>
26+
27+
### Example 2
28+
29+
<CodeTab labels={["ReScript", "JS Output"]}>
30+
31+
```res
32+
type bit = [#0 | #1]
33+
let bit: bit = #1
34+
let value = (bit :> int)
35+
```
36+
37+
```js
38+
var bit = 1;
39+
var value = 1;
40+
```
41+
42+
</CodeTab>
43+
44+
### Example 3
45+
46+
<CodeTab labels={["ReScript", "JS Output"]}>
47+
48+
```res
49+
type person = {"id": int, "name": string}
50+
type name = {"name": string}
51+
let person = {"id": 10, "name": "Gideon"}
52+
let name = (person :> name)
53+
```
54+
55+
```js
56+
var person = {
57+
id: 10,
58+
name: "Gideon",
59+
};
60+
61+
var name = person;
62+
```
63+
64+
</CodeTab>
65+
66+
67+
### References
68+
69+
* [Polymorphic Variant Coercion](/docs/manual/latest/polymorphic-variant#coercion)

0 commit comments

Comments
 (0)