Skip to content

Commit 1e336f0

Browse files
authored
Merge pull request #323 from kevanstannard/syntax-lookup-type-parameter
Syntax lookup: Type parameter
2 parents 722470a + d8a47a7 commit 1e336f0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
id: "type-parameter"
3+
keywords: ["type", "parameter", "generics", "placeholder"]
4+
name: "'a"
5+
summary: "This is a `type parameter`."
6+
category: "languageconstructs"
7+
---
8+
9+
Types may be declared with [type parameters](/docs/manual/latest/type#type-parameter-aka-generic) (also known as _generics_ in other languages) to create generalized versions of those types.
10+
11+
### Example with one type parameter
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res
16+
type point2D<'a> = ('a, 'a)
17+
18+
let intPoint: point2D<int> = (1, 2)
19+
let floatPoint: point2D<float> = (1.0, 2.0)
20+
```
21+
22+
```js
23+
var intPoint = [1, 2];
24+
var floatPoint = [1.0, 2.0];
25+
```
26+
27+
</CodeTab>
28+
29+
### Example with multiple type parameters
30+
31+
<CodeTab labels={["ReScript", "JS Output"]}>
32+
33+
```res
34+
type either<'a, 'b> = This('a) | That('b)
35+
36+
let value1: either<int, string> = This(123)
37+
let value2: either<int, string> = That("Hello")
38+
```
39+
40+
```js
41+
var value1 = {
42+
TAG: /* This */ 0,
43+
_0: 123,
44+
};
45+
46+
var value2 = {
47+
TAG: /* That */ 1,
48+
_0: "Hello",
49+
};
50+
```
51+
52+
</CodeTab>
53+
54+
### References
55+
56+
- [Type parameters](/docs/manual/latest/type#type-parameter-aka-generic)

0 commit comments

Comments
 (0)