Skip to content

Commit e8cb31d

Browse files
Add docs and syntax lookup for scoped polymorphic types (#693)
1 parent 401e476 commit e8cb31d

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

data/sidebar_manual_latest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"reserved-keywords"
3636
],
3737
"Advanced Features": [
38-
"extensible-variant"
38+
"extensible-variant",
39+
"scoped-polymorphic-types"
3940
],
4041
"JavaScript Interop": [
4142
"interop-cheatsheet",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
id: "scoped-polymorphic-type"
3+
keywords: ["scoped", "polymorphic"]
4+
name: "'a."
5+
summary: "This is a `scoped polymorphic type`."
6+
category: "languageconstructs"
7+
---
8+
9+
In ReScript, polymorphic functions can only deal with one specific type. When they are called, their type gets fixed.
10+
For instance this logging function is bound in a polymorphic way as you can see by the type parameter (`'a`).
11+
12+
```res
13+
type logger<'a> = { log: 'a => unit}
14+
15+
@module("jsAPI") external getLogger: unit => logger<'a> = "getLogger"
16+
17+
let myLogger = getLogger()
18+
19+
myLogger.log("Hello, ReScript!")
20+
myLogger.log(42) // Type error!
21+
```
22+
23+
Scoped polymorphic types make such behavior possible, in a type-safe way. See the `'a.` in the example below.
24+
25+
### Example
26+
27+
<CodeTab labels={["ReScript", "JS Output"]}>
28+
29+
```res
30+
type logger = { log: 'a. 'a => unit}
31+
32+
@module("jsAPI") external getLogger: unit => logger = "getLogger"
33+
34+
let myLogger = getLogger()
35+
36+
myLogger.log("Hello, ReScript!")
37+
myLogger.log(42) // Ok!
38+
```
39+
40+
```js
41+
var JsAPI = require("jsAPI");
42+
43+
var myLogger = JsAPI.getLogger();
44+
45+
myLogger.log("Hello, ReScript!");
46+
47+
myLogger.log(42);
48+
```
49+
50+
</CodeTab>
51+
52+
### References
53+
54+
- [Scoped Polymorphic Types](/docs/manual/latest/scoped-polymorphic-types)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: "Scoped Polymorphic Types"
3+
description: "Scoped Polymorphic Types in ReScript"
4+
canonical: "/docs/manual/latest/scoped-polymorphic-types"
5+
---
6+
7+
# Scoped Polymorphic Types
8+
9+
Scoped Polymorphic Types in ReScript are functions with the capability to handle arguments of any type within a specific scope. This feature is particularly valuable when working with JavaScript APIs, as it allows your functions to accommodate diverse data types while preserving ReScript's strong type checking.
10+
11+
## Definition and Usage
12+
13+
Scoped polymorphic types in ReScript offer a flexible and type-safe way to handle diverse data types within specific scopes. This documentation provides an example to illustrate their usage in a JavaScript context.
14+
15+
### Example: Logging API
16+
17+
Consider a logging example within a JavaScript context that processes various data types:
18+
19+
```js
20+
const logger = {
21+
log: (data) => {
22+
if (typeof data === "string") {
23+
/* handle string */
24+
} else if (typeof data === "number") {
25+
/* handle number */
26+
} else {
27+
/* handle other types */
28+
}
29+
},
30+
};
31+
```
32+
33+
In ReScript, we can bind to this function as a record with a scoped polymorphic function type:
34+
35+
```res prelude
36+
type logger = { log: 'a. 'a => unit }
37+
38+
@module("jsAPI") external getLogger: unit => logger = "getLogger"
39+
```
40+
41+
The `logger` type represents a record with a single field `log`, which is a scoped polymorphic function type `'a. 'a => unit`. The `'a` indicates a type variable that can be any type within the scope of the `log` function.
42+
43+
Now, we can utilize the function obtained from `getLogger`:
44+
45+
<CodeTab labels={["ReScript", "JS Output"]}>
46+
47+
```res example
48+
let myLogger = getLogger()
49+
50+
myLogger.log("Hello, ReScript!")
51+
myLogger.log(42)
52+
```
53+
54+
```js
55+
var myLogger = JsAPI.getLogger();
56+
57+
myLogger.log("Hello, ReScript!");
58+
myLogger.log(42);
59+
```
60+
61+
</CodeTab>
62+
63+
In this example, we create an instance of the logger by calling `getLogger()`, and then we can use the `log` function on the `myLogger` object to handle different data types.
64+
65+
## Limitations of Normal Polymorphic Types
66+
67+
Let's consider the same logging example in ReScript, but this time using normal polymorphic types:
68+
69+
```res
70+
type logger<'a> = { log: 'a => unit}
71+
72+
@module("jsAPI") external getLogger: unit => logger<'a> = "getLogger"
73+
````
74+
75+
In this case, the `logger` type is a simple polymorphic function type `'a => unit`. However, when we attempt to use this type in the same way as before, we encounter an issue:
76+
77+
```res
78+
let myLogger = getLogger()
79+
80+
myLogger.log("Hello, ReScript!")
81+
myLogger.log(42) // Type error!
82+
```
83+
84+
The problem arises because the type inference in ReScript assigns a concrete type to the `logger` function based on the first usage. In this example, after the first call to `myLogger`, the compiler infers the type `logger<string>` for `myLogger`. Consequently, when we attempt to pass an argument of type `number` in the next line, a type error occurs because it conflicts with the inferred type `logger<string>`.
85+
86+
In contrast, scoped polymorphic types, such as `'a. 'a => unit`, overcome this limitation by allowing type variables within the scope of the function. They ensure that the type of the argument is preserved consistently within that scope, regardless of the specific value used in the first invocation.

0 commit comments

Comments
 (0)