Skip to content
Merged
Changes from all 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
56 changes: 56 additions & 0 deletions misc_docs/syntax/language_type_parameter.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
id: "type-parameter"
keywords: ["type", "parameter", "generics", "placeholder"]
name: "'a"
summary: "This is a `type parameter`."
category: "languageconstructs"
---

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.

### Example with one type parameter

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

```res
type point2D<'a> = ('a, 'a)

let intPoint: point2D<int> = (1, 2)
let floatPoint: point2D<float> = (1.0, 2.0)
```

```js
var intPoint = [1, 2];
var floatPoint = [1.0, 2.0];
```

</CodeTab>

### Example with multiple type parameters

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

```res
type either<'a, 'b> = This('a) | That('b)

let value1: either<int, string> = This(123)
let value2: either<int, string> = That("Hello")
```

```js
var value1 = {
TAG: /* This */ 0,
_0: 123,
};

var value2 = {
TAG: /* That */ 1,
_0: "Hello",
};
```

</CodeTab>

### References

- [Type parameters](/docs/manual/latest/type#type-parameter-aka-generic)