Skip to content

Commit 5498056

Browse files
committed
docs: add page about opaque types
1 parent 00a9390 commit 5498056

4 files changed

Lines changed: 130 additions & 1 deletion

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: "Opaque Types"
3+
description: "Opaque types syntax and examples for ReScript"
4+
canonical: "/docs/manual/opaque-types"
5+
section: "Language Features"
6+
order: 29
7+
---
8+
9+
An "opaque type" is a powerful language feature that allows you to hide implementation details from outside a module and to add meaning and safety to primitive types like `string` or `int`.
10+
11+
Real software often has data that needs to be more than just a `string` or an `int`. We have email address, numbers between 1-100, auth tokens, etc... Most of the time we can just say that `type email = string` and move on, but we leave ourselves open to bugs this way. Is `""` a valiud email? Is `-1` a number between 1-00? No. We can add in runtime checks to prevent against these issues, but ReScript can also use opaque types to add type level guardrails for these types of data.
12+
13+
Let's start with out email example using an imaginary `EmailApi` that sends a message to an email address we provide it.
14+
15+
```res
16+
let sendEmail = (email: string, message: string) => EmailAPI.send(email, string)
17+
```
18+
19+
We would get failures is we tried to send a message to an invalid email address.
20+
21+
```res
22+
sendEmail("not_an_email**123")
23+
```
24+
25+
By using an opaque type we can only allow this function to accept valid email addresses. To create an opaque type you must either create a typed module definition or use a `.resi` file. To make a type opaque we simply provide details to the module itself and limit what we expose through the modules type declaration. By using the `private` keyword in the `.resi` file or module type we can choose if we want outside consumers to see what the primitive type is while still keeping it opaque.
26+
27+
```res
28+
module Email: {
29+
type t // type definition will show up as `type Email.t`
30+
} = {
31+
type t = string
32+
}
33+
```
34+
35+
```res
36+
module Email: {
37+
type t = private string // type definition will show up as `type Email.t = string`
38+
} = {
39+
type t = string
40+
}
41+
```
42+
43+
Or with `.resi` files.
44+
45+
```res
46+
// Email.res
47+
type t = string
48+
```
49+
50+
```res
51+
// Email.resi
52+
type t // or type t = private string
53+
```
54+
55+
Any of these approaches are valid and it depends on what internal details you want to be visible and if you prefer interface files or module type definitions.
56+
57+
Now let's refactor our `sendEmail` function to use our opaque type.
58+
59+
```res
60+
module Email: {
61+
type t // type definition will show up as `type Email.t`
62+
let sendEmail: (t, string) => unit
63+
} = {
64+
type t = string
65+
let sendEmail = (email: t, message: t) => EmailAPI.send((email :> string), string)
66+
}
67+
```
68+
69+
`:>` is the type coercion operator It cannot be used to change a type to a different primitive value, such as `(42 :> string)`, but we can use it to allow our opaque type to be passed to functions that work on the primitive type that it's based on. Once this is done, the value is now longer the opaque type and it will become the type we coerced it to.n We can always use an opaque type as a primitive type, but we cannot use a primitive type as an opaque type.
70+
71+
We will see a type error if we try and send a primitive `string` to our `sendEmail` function.
72+
73+
```res
74+
let handleSend = (email: string) => Email.sendEmail(email, "Welcome!") // ERROR! This has type: string But this function argument is expecting: Email.t
75+
```
76+
77+
But how can we create an `Email.t` now? We have to add a `make` function to our `Email` module that will validate if a string is a valid email.
78+
79+
```res
80+
module Email: {
81+
type t
82+
let make: string => Result.t<t, JsError.t>
83+
let sendEmail: (t, string) => unit
84+
} = {
85+
type t = string
86+
let emailRegex = /^[^@]+@[^@]+\.[^@]+$/
87+
let make = (unvalidatedEmail: string) => {
88+
switch emailRegex->RegExp.test(unvalidatedEmail) {
89+
| true => Ok(unvalidatedEmail)
90+
| false => Error(JsError.make("Invalid email address"))
91+
}
92+
}
93+
let sendEmail = (email: t, message: string) => EmailAPI.send((email :> string), message)
94+
}
95+
```
96+
97+
When we write our `handleSend` function we now are forced to deal with situation where the email not be valid before we even try to send it.
98+
99+
```res
100+
let handleSend = (email: string) =>
101+
switch email->Email.make {
102+
| Ok(email) => Email.sendEmail(email, "Welcome!")
103+
| Error(error) => Console.error(error)
104+
}
105+
```
106+
107+
You usually want to try and validate opaque types near the edges of the program, such as reading from a database or user input, so you don't have to make constant checks throughout your code.

markdown-pages/syntax-lookup/operators_type_coercion.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ summary: "This is the `type coercion` operator."
1515
category: "operators"
1616
---
1717

18-
The `:>` operator may be used to convert a polymorphic variant to a `string` or `int`, or convert an [object](../docs/manual/object.mdx) to a type with a subset of its fields.
18+
The `:>` operator may be used to convert a polymorphic variant to a `string` or `int`, to downcast an [opaque type](../docs/manual/opaque-types.mdx) to it's primitive type, or convert an [object](../docs/manual/object.mdx) to a type with a subset of its fields.
1919

2020
_Since ReScript `11.0.0`_ coercion also works for converting
2121

src/Email.res

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Email: {
2+
type t
3+
let make: string => Result.t<t, JsError.t>
4+
let sendEmail: (t, string) => unit
5+
} = {
6+
type t = string
7+
let emailRegex = /^[^@]+@[^@]+\.[^@]+$/
8+
let make = (unvalidatedEmail: string) => {
9+
switch emailRegex->RegExp.test(unvalidatedEmail) {
10+
| true => Ok(unvalidatedEmail)
11+
| false => Error(JsError.make("Invalid email address"))
12+
}
13+
}
14+
let sendEmail = (email: t, message: string) => Console.log2((email :> string), message)
15+
}
16+
17+
let handleSend = (email: string) =>
18+
switch email->Email.make {
19+
| Ok(email) => Email.sendEmail(email, "Welcome!")
20+
| Error(error) => Console.error(error)
21+
}

src/Foo.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)