|
| 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