Skip to content
Draft
Show file tree
Hide file tree
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
107 changes: 107 additions & 0 deletions markdown-pages/docs/manual/opaque-types.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: "Opaque Types"
description: "Opaque types syntax and examples for ReScript"
canonical: "/docs/manual/opaque-types"
section: "Language Features"
order: 29
---

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

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.

Let's start with out email example using an imaginary `EmailApi` that sends a message to an email address we provide it.

```res

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add validation markers to the new code blocks

Every code fence on this page is plain res, so scripts/test-examples.mjs skips the entire page because it only parses res example, res prelude, and res sig blocks. As a result, the invalid calls on lines 16, 22, and 65 pass yarn test; mark the intended-valid snippets with the supported fence types and provide any required prelude so documentation examples remain compiler-checked.

AGENTS.md reference: AGENTS.md:L195-L195

Useful? React with 👍 / 👎.

let sendEmail = (email: string, message: string) => EmailAPI.send(email, string)
```

We would get failures is we tried to send a message to an invalid email address.

```res
sendEmail("not_an_email**123")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Supply the message in the invalid-email call

This call cannot demonstrate a runtime failure for an invalid address because the preceding sendEmail function takes two unlabelled arguments, while this invocation supplies only one. ReScript 12 rejects it for incorrect arity before EmailAPI.send can run, so pass a message argument here.

Useful? React with 👍 / 👎.

```

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.

```res
module Email: {
type t // type definition will show up as `type Email.t`
} = {
type t = string
}
```

```res
module Email: {
type t = private string // type definition will show up as `type Email.t = string`
} = {
type t = string
}
```

Or with `.resi` files.

```res
// Email.res
type t = string
```

```res
// Email.resi
type t // or type t = private string
```

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.

Now let's refactor our `sendEmail` function to use our opaque type.

```res
module Email: {
type t // type definition will show up as `type Email.t`
let sendEmail: (t, string) => unit
} = {
type t = string
let sendEmail = (email: t, message: t) => EmailAPI.send((email :> string), string)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pass the message value instead of the string type

The refactored implementation passes string as the second argument, but that is a type name rather than a value; ReScript reports that the value string cannot be found. It also unnecessarily annotates message as the email type t; use message: string and pass message to EmailAPI.send, and make the equivalent value correction in the earlier example on line 16.

Useful? React with 👍 / 👎.

}
```

`:>` 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Repair the garbled coercion explanation

The central explanation says the coerced expression is “now longer” opaque and ends the next clause with the stray text to.n, which reverses and obscures the intended statement. Change this to “no longer the opaque type” and remove the stray character so readers can understand what type the coerced expression has.

Useful? React with 👍 / 👎.


We will see a type error if we try and send a primitive `string` to our `sendEmail` function.

```res
let handleSend = (email: string) => Email.sendEmail(email, "Welcome!") // ERROR! This has type: string But this function argument is expecting: Email.t
```

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.

```res
module Email: {
type t
let make: string => Result.t<t, JsError.t>
let sendEmail: (t, string) => unit
} = {
type t = string
let emailRegex = /^[^@]+@[^@]+\.[^@]+$/
let make = (unvalidatedEmail: string) => {
switch emailRegex->RegExp.test(unvalidatedEmail) {
| true => Ok(unvalidatedEmail)
| false => Error(JsError.make("Invalid email address"))
}
}
let sendEmail = (email: t, message: string) => EmailAPI.send((email :> string), message)
}
```

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.

```res
let handleSend = (email: string) =>
switch email->Email.make {
| Ok(email) => Email.sendEmail(email, "Welcome!")
| Error(error) => Console.error(error)
}
```

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.
2 changes: 1 addition & 1 deletion markdown-pages/syntax-lookup/operators_type_coercion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ summary: "This is the `type coercion` operator."
category: "operators"
---

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Limit opaque coercion to private manifest types

This new description is false for the page's primary type t form: outside a module that exports an abstract type t, ReScript 12 rejects (value :> string) because the representation is unknown. The coercion is available when the interface exposes type t = private string; distinguish that private manifest form instead of promising that any opaque type can be downcast.

AGENTS.md reference: AGENTS.md:L59-L59

Useful? React with 👍 / 👎.


_Since ReScript `11.0.0`_ coercion also works for converting

Expand Down
Loading