diff --git a/misc_docs/syntax/decorator_uncurry.mdx b/misc_docs/syntax/decorator_uncurry.mdx new file mode 100644 index 000000000..9436bf327 --- /dev/null +++ b/misc_docs/syntax/decorator_uncurry.mdx @@ -0,0 +1,36 @@ +--- +id: "uncurry-decorator" +keywords: ["uncurry", "decorator"] +name: "@uncurry" +summary: "This is the `@uncurry` decorator." +category: "decorators" +--- + +The `@uncurry` decorator can be used to mark any callback argument within an **external** function as an uncurried function without the need for any [explicit uncurried function syntax](/docs/manual/latest/function#uncurried-function) `((.) => { ... })`. + +### Example + +In the following example we are binding to a function `map(arr, callback)` and use the `@uncurry` annotation to make sure that `callback` is always treated as an uncurried function: + + + +```res +@bs.send +external map: (array<'a>, @uncurry ('a => 'b)) => array<'b> = "map" + +let result = map([1, 2, 3], x => x + 1) +``` + +```js +var result = [1, 2, 3].map(function (x) { + return (x + 1) | 0 +}) +``` + + + +As we can see, we defined a regular function `('a) => 'b` instead of `(. 'a) => 'b`, but still have all the guarantees. Please note that the compiler does a lot of optimizations, so the example above will compile to the same code even without the `@uncurry` decorator. Adding the decorator (or using the `(.) =>` syntax) makes the output 100% predictable though. + +### References + +- [Use Guaranteed Uncurrying](/docs/manual/latest/bind-to-js-function#solution-use-guaranteed-uncurrying)