Skip to content

Commit 43f4439

Browse files
Format
1 parent 156ad3c commit 43f4439

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

_blogposts/2025-09-01-let-unwrap.mdx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ date: "2025-09-01"
44
badge: roadmap
55
title: let?
66
description: |
7-
A new let-unwrap syntax just landed in ReScript. Experimental!
7+
A new let-unwrap syntax just landed in ReScript. Experimental!
88
---
99

1010
After long discussions we finally decided on an unwrap syntax for both the `option` and `result` types that we are happy with and that still matches the explicitness of ReScript we all like.
1111

12-
`let?` or `let-unwrap` is a tiny syntax that unwraps `result`/`option` values and *early-returns* on `Error`/`None`. It’s explicitly **experimental** and **disabled by default** behind a new "experimental features" gate. See below how to enable it.
12+
`let?` or `let-unwrap` is a tiny syntax that unwraps `result`/`option` values and _early-returns_ on `Error`/`None`. It’s explicitly **experimental** and **disabled by default** behind a new "experimental features" gate. See below how to enable it.
1313

1414
Before showing off this new feauture, let's explore why it is useful. Consider a chain of `async` functions that are dependent on the result of the previous one. The naive way to write this in modern ReScript with `async`/`await` is to just `switch` on the results.
1515

@@ -29,9 +29,10 @@ let getUser = async id =>
2929
}
3030
```
3131

32-
Two observations:
33-
1. with every `switch` expression, this function gets nested deeper.
34-
2. The `Error` branch of every `switch` is just an identity mapper (neither wrapper nor contents change).
32+
Two observations:
33+
34+
1. with every `switch` expression, this function gets nested deeper.
35+
2. The `Error` branch of every `switch` is just an identity mapper (neither wrapper nor contents change).
3536

3637
The only alternative in ReScript was always to use some specialized methods:
3738

@@ -65,7 +66,7 @@ With `let?`, we can now safely focus on the the happy-path in the scope of the f
6566

6667
This desugars to a **sequence** of early-returns that you’d otherwise write by hand, so there’s **no extra runtime cost** and it plays nicely with `async/await` as the example above suggests.
6768

68-
Of course, it also works for `option` with `Some(...)`.
69+
Of course, it also works for `option` with `Some(...)`.
6970

7071
```rescript
7172
let getActiveUser = user => {
@@ -74,7 +75,7 @@ let getActiveUser = user => {
7475
}
7576
```
7677

77-
It also works with the unhappy path, with `Error(...)` or `None` as the main type and `Ok(...)` or `Some(...)` as the implicitly mapped types.
78+
It also works with the unhappy path, with `Error(...)` or `None` as the main type and `Ok(...)` or `Some(...)` as the implicitly mapped types.
7879

7980
```rescript
8081
let getNoUser = user => {
@@ -88,11 +89,11 @@ let decodeUserWithHumanReadableError = user => {
8889
}
8990
```
9091

91-
Beware it targets built-ins only, namely `result` and `option`. Custom variants still need `switch`. And it is for block or local bindings only; top-level usage is rejected.
92+
Beware it targets built-ins only, namely `result` and `option`. Custom variants still need `switch`. And it is for block or local bindings only; top-level usage is rejected.
9293

9394
```rescript
9495
let? Ok(user) = await fetchUser("1")
95-
// ^^^^^^^ ERROR: `let?` is not allowed for top-level bindings.
96+
// ^^^^^^^ ERROR: `let?` is not allowed for top-level bindings.
9697
```
9798

9899
<!-- TODO: demonstrate error handling with polymorphic variants a little more -->
@@ -120,5 +121,3 @@ If you still use the legacy build system, enable it with the compiler flag `-ena
120121
We would love to hear your thoughts about this feature in the [forum](https://forum.rescript-lang.org/). Please try it out and tell us what you think!
121122

122123
Happy hacking!
123-
124-

0 commit comments

Comments
 (0)