From 65d40328faf63061adf04e1481a109603fe842d1 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Tue, 14 Nov 2023 14:18:50 +0100 Subject: [PATCH] Uncurried Mode docs --- data/sidebar_manual_latest.json | 1 + pages/docs/manual/latest/uncurried-mode.mdx | 131 ++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 pages/docs/manual/latest/uncurried-mode.mdx diff --git a/data/sidebar_manual_latest.json b/data/sidebar_manual_latest.json index 98e0694c4..83ee9294a 100644 --- a/data/sidebar_manual_latest.json +++ b/data/sidebar_manual_latest.json @@ -32,6 +32,7 @@ "import-export", "attribute", "unboxed", + "uncurried-mode", "reserved-keywords" ], "Advanced Features": [ diff --git a/pages/docs/manual/latest/uncurried-mode.mdx b/pages/docs/manual/latest/uncurried-mode.mdx new file mode 100644 index 000000000..c139f3c24 --- /dev/null +++ b/pages/docs/manual/latest/uncurried-mode.mdx @@ -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: + + + +```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; +} +``` + + + +It is shorter than having to write all remaining parameters again. + + + +```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; +} +``` + + + +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. + + +```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; +} +``` + + +Or use the new explicit syntax for partial application. + + +```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; +} +``` + + +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. \ No newline at end of file