Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncurried Mode docs #748

Merged
Merged
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
1 change: 1 addition & 0 deletions data/sidebar_manual_latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"import-export",
"attribute",
"unboxed",
"uncurried-mode",
"reserved-keywords"
],
"Advanced Features": [
Expand Down
131 changes: 131 additions & 0 deletions pages/docs/manual/latest/uncurried-mode.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
title: "Uncurried Mode"
description: "Uncurried Mode"
canonical: "/docs/manual/latest/uncurried-mode"
---

# Uncurried Mode

Since ReScript 11, the language compiles in "uncurried" mode by default.
Before that, currying in ReScript looked like this:

<CodeTab labels={["ReScript", "JS Output"]}>

```res
let add = (a, b) => a + b
let addFive = add(5)
```

```js
function add(a, b) {
return a + b | 0;
}

function addFive(param) {
return 5 + param | 0;
}
```

</CodeTab>

It is shorter than having to write all remaining parameters again.

<CodeTab labels={["ReScript", "JS Output"]}>

```res
let add = (a, b) => a + b
let addFive = (b) => add(5, b)
```

```js
function add(a, b) {
return a + b | 0;
}

function addFive(b) {
return 5 + b | 0;
}
```

</CodeTab>

In ReScript 11, that would yield an error.

```rescript
let add = (a, b) => a + b
let addFive = add(5) // <-- Error:
// This uncurried function has type (. int, int) => int
// It is applied with 1 arguments but it requires 2.
```

To fix it, you can just state the remaining parameters explicitly.

<CodeTab labels={["ReScript", "JS Output"]}>
```res
let add = (a, b) => a + b
let addFive = (b) => add(5, b)
```

```js
function add(a, b) {
return a + b | 0;
}

function addFive(b) {
return 5 + b | 0;
}
```
</CodeTab>

Or use the new explicit syntax for partial application.

<CodeTab labels={["ReScript", "JS Output"]}>
```res
let add = (a, b) => a + b
let addFive = add(5, ...)
```

```js
function add(a, b) {
return a + b | 0;
}

function addFive(extra) {
return 5 + extra | 0;
}
```
</CodeTab>

The former approach helps library authors support both ReScript 11 and earlier versions.

### No final unit necessary

In Uncurried Mode, the "final unit" pattern is not necessary anymore, while optional or default parameters are still supported.

```res
// old
let myFun = (~name=?, ())

// new
let myFun = (~name=?)
```

### How to switch back to curried mode

While we strongly encourage all users to switch to the new uncurried mode, it is still possible to opt out. Just add a

```json
{
"uncurried": false
}
```

to your `rescript.json`, and your project will be compiled in curried mode again.

If you have uncurried mode off and still want to try it on a per-file basis, you can turn it on via

```rescript
@@uncurried
```

at the top of a `.res` file.
Loading