Skip to content

Commit 4eeb101

Browse files
Add blogpost about uncurried mode (#714)
* Add blogpost about uncurried mode * Some adaptions according to PR comments * Set uncurried blogpost date to today
1 parent 53ce013 commit 4eeb101

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
author: rescript-team
3+
date: "2023-09-18"
4+
title: Uncurried Mode
5+
badge: roadmap
6+
description: |
7+
A tour of new capabilities coming to ReScript v11
8+
---
9+
10+
> This is the fourth post covering new capabilities that'll ship in ReScript v11. You can check out the first post on [Better Interop with Customizable Variants](/blog/improving-interop), the second post on [Enhanced Ergonomics for Record Types](/blog/enhanced-ergonomics-for-record-types) and the third post on [First-class Dynamic Import Support](/blog/first-class-dynamic-import-support).
11+
12+
## Introduction
13+
14+
ReScript is a language that strives to keep its users free from experiencing runtime errors. Usually, when a program compiles, it will already do what the user described.
15+
But there is still a concept in the language that makes it easy to let some errors slip through. Currying!
16+
17+
Because of currying, partial application of functions is possible. That feature is always advertised as something really powerful. For instance,
18+
19+
```rescript
20+
let add = (a, b) => a + b
21+
let addFive = add(5)
22+
```
23+
24+
is shorter than having to write all remaining parameters again
25+
26+
```rescript
27+
let add = (a, b) => a + b
28+
let addFive = (b) => add(5, b)
29+
```
30+
31+
This comes at a price though. Here are some examples to show the drawbacks of currying:
32+
33+
* Errors because of changed function signatures have their impact at the use site. Consider this example, where the signature of the onChange function is extended with
34+
a labeled argument
35+
```diff
36+
@react.component
37+
- let make = (~onChange: string => option<unit => unit>) => {
38+
+ let make = (~onChange: (~a: int, string) => option<unit => unit>) => {
39+
React.useEffect(() => {
40+
// As partial application is allowed, there is no error here.
41+
let cleanup = onChange("change")
42+
43+
// Here it errors with "This call is missing an argument of type (~a: int)"
44+
cleanup
45+
})
46+
}
47+
```
48+
* If you wanted explicitly uncurry a function, you needed to annotate it with the uncurried dot.
49+
```rescript
50+
(. param) => ()
51+
```
52+
* As ReScript could not fully statically analyze when to automatically uncurry a function over multiple files, it led to unnecessary `Curry.` calls in the emitted JavaScript code.
53+
* In the standard library (`Belt`), there are both curried and uncurried versions of the same function so you were required to think for yourself when to use the uncurried version and when only the curried one will work.
54+
* In combination with `ignore` / `let _ = ...`, curried can lead to unexpected behavior at runtime after adding a parameter to a function, because you are accidentally ignoring the result of a partial evaluation so that the function is not called at all.
55+
1. Have a look at this simple function. It is assigned to `_` because we ignore the resulting `string` value.
56+
```res
57+
let myCurriedFn = (~first) => first
58+
let _ = myCurriedFn(~first="Hello!")
59+
// ^ string
60+
```
61+
2. Now the function got a second parameter `~second`. Here, the resulting value is a function, which means it is not fully applied and thus never executed.
62+
```res
63+
let myCurriedFn = (~first, ~second) => first ++ " " ++ second
64+
let _ = myCurriedFn(~first="Hello!")
65+
// ^ (~second: string) => string
66+
```
67+
3. One way to prevent such errors is to annotate the underscore with the function's return type:
68+
```res
69+
let _: string = myCurriedFn(~first="Hello!")
70+
```
71+
4. However, the same issue arises when using the built-in ignore function, which cannot be annotated:
72+
```res
73+
myCurriedFn(~first="Hello!")->ignore
74+
```
75+
76+
Those are all only some small paper cuts, but all of them are intricacies that make the language harder to learn.
77+
78+
## Uncurried mode
79+
80+
Starting with ReScript 11, your code will be compiled in uncurried mode. Yes, there is still a way to turn it off ([see below](#how-to-switch-back-to-curried-mode)), but we have decided to already default to this behavior to make it easier for newcomers.
81+
In uncurried mode, the introductory example yields an error:
82+
83+
```rescript
84+
let add = (a, b) => a + b
85+
let addFive = add(5) // <-- Error:
86+
// This uncurried function has type (. int, int) => int
87+
// It is applied with 1 arguments but it requires 2.
88+
```
89+
90+
to fix it, you have two options:
91+
92+
1. state the remaining parameters explicitly
93+
```rescript
94+
let add = (a, b) => a + b
95+
let addFive = (b) => add(5, b)
96+
```
97+
2. or use the new explicit syntax for partial application
98+
```rescript
99+
let add = (a, b) => a + b
100+
let addFive = add(5, ...)
101+
```
102+
103+
The former approach helps library authors support both ReScript 11 and earlier versions.
104+
105+
### No final unit anymore
106+
107+
We are happy to announce that with uncurried mode the "final unit" pattern is not necessary anymore, while you still can use optional or default parameters.
108+
109+
```res
110+
// old
111+
let myFun = (~name=?, ())
112+
113+
// new
114+
let myFun = (~name=?)
115+
```
116+
117+
### More wins
118+
119+
Furthermore, function calls in uncurried mode are now guaranteed to get compiled as simple JavaScript function calls, which is quite nice for readability of the generated code.
120+
It may also give you some (negligible) performance gains.
121+
122+
### How to switch back to curried mode
123+
124+
While we strongly encourage all users to switch to the new uncurried mode, it is still possible to opt out. Just add a
125+
126+
```json
127+
{
128+
"uncurried": false
129+
}
130+
```
131+
132+
to your `bsconfig.json` (), and your project will be compiled in curried mode again.
133+
134+
If you have uncurried mode off and still want to try it on a per-file basis, you can turn it on via
135+
136+
```rescript
137+
@@uncurried
138+
```
139+
140+
at the top of a `.res` file.
141+
142+
## Conclusion
143+
144+
Many thoughts have led to this decision, but we think this change is a great fit for a compile-to-JS language overall. If you are interested in the details, have a look at the corresponding [forum post](https://forum.rescript-lang.org/t/uncurried-by-default/) and its comments.
145+
146+
We hope that this new way of writing ReScript will make it both easier for beginners and also more enjoyable for the seasoned developers.
147+
148+
As always, we're eager to hear about your experiences with our new features. Feel free to share your thoughts and feedback with us on our [issue tracker](https://github.com/rescript-lang/rescript-compiler/issues) or on the [forum](https://forum.rescript-lang.org).
149+
150+
Happy hacking!

0 commit comments

Comments
 (0)