Skip to content

Commit 65d4032

Browse files
Uncurried Mode docs
1 parent 183becd commit 65d4032

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

data/sidebar_manual_latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"import-export",
3333
"attribute",
3434
"unboxed",
35+
"uncurried-mode",
3536
"reserved-keywords"
3637
],
3738
"Advanced Features": [
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: "Uncurried Mode"
3+
description: "Uncurried Mode"
4+
canonical: "/docs/manual/latest/uncurried-mode"
5+
---
6+
7+
# Uncurried Mode
8+
9+
Since ReScript 11, the language compiles in "uncurried" mode by default.
10+
Before that, currying in ReScript looked like this:
11+
12+
<CodeTab labels={["ReScript", "JS Output"]}>
13+
14+
```res
15+
let add = (a, b) => a + b
16+
let addFive = add(5)
17+
```
18+
19+
```js
20+
function add(a, b) {
21+
return a + b | 0;
22+
}
23+
24+
function addFive(param) {
25+
return 5 + param | 0;
26+
}
27+
```
28+
29+
</CodeTab>
30+
31+
It is shorter than having to write all remaining parameters again.
32+
33+
<CodeTab labels={["ReScript", "JS Output"]}>
34+
35+
```res
36+
let add = (a, b) => a + b
37+
let addFive = (b) => add(5, b)
38+
```
39+
40+
```js
41+
function add(a, b) {
42+
return a + b | 0;
43+
}
44+
45+
function addFive(b) {
46+
return 5 + b | 0;
47+
}
48+
```
49+
50+
</CodeTab>
51+
52+
In ReScript 11, that would yield an error.
53+
54+
```rescript
55+
let add = (a, b) => a + b
56+
let addFive = add(5) // <-- Error:
57+
// This uncurried function has type (. int, int) => int
58+
// It is applied with 1 arguments but it requires 2.
59+
```
60+
61+
To fix it, you can just state the remaining parameters explicitly.
62+
63+
<CodeTab labels={["ReScript", "JS Output"]}>
64+
```res
65+
let add = (a, b) => a + b
66+
let addFive = (b) => add(5, b)
67+
```
68+
69+
```js
70+
function add(a, b) {
71+
return a + b | 0;
72+
}
73+
74+
function addFive(b) {
75+
return 5 + b | 0;
76+
}
77+
```
78+
</CodeTab>
79+
80+
Or use the new explicit syntax for partial application.
81+
82+
<CodeTab labels={["ReScript", "JS Output"]}>
83+
```res
84+
let add = (a, b) => a + b
85+
let addFive = add(5, ...)
86+
```
87+
88+
```js
89+
function add(a, b) {
90+
return a + b | 0;
91+
}
92+
93+
function addFive(extra) {
94+
return 5 + extra | 0;
95+
}
96+
```
97+
</CodeTab>
98+
99+
The former approach helps library authors support both ReScript 11 and earlier versions.
100+
101+
### No final unit necessary
102+
103+
In Uncurried Mode, the "final unit" pattern is not necessary anymore, while optional or default parameters are still supported.
104+
105+
```res
106+
// old
107+
let myFun = (~name=?, ())
108+
109+
// new
110+
let myFun = (~name=?)
111+
```
112+
113+
### How to switch back to curried mode
114+
115+
While we strongly encourage all users to switch to the new uncurried mode, it is still possible to opt out. Just add a
116+
117+
```json
118+
{
119+
"uncurried": false
120+
}
121+
```
122+
123+
to your `rescript.json`, and your project will be compiled in curried mode again.
124+
125+
If you have uncurried mode off and still want to try it on a per-file basis, you can turn it on via
126+
127+
```rescript
128+
@@uncurried
129+
```
130+
131+
at the top of a `.res` file.

0 commit comments

Comments
 (0)