Skip to content

Commit eaeeb35

Browse files
authored
Merge pull request #179 from kevanstannard/uncurry-decorator-syntax
Add @uncurry decorator syntax lookup
2 parents f519649 + 416ecff commit eaeeb35

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
id: "uncurry-decorator"
3+
keywords: ["uncurry", "decorator"]
4+
name: "@uncurry"
5+
summary: "This is the `@uncurry` decorator."
6+
category: "decorators"
7+
---
8+
9+
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) `((.) => { ... })`.
10+
11+
### Example
12+
13+
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:
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
@bs.send
19+
external map: (array<'a>, @uncurry ('a => 'b)) => array<'b> = "map"
20+
21+
let result = map([1, 2, 3], x => x + 1)
22+
```
23+
24+
```js
25+
var result = [1, 2, 3].map(function (x) {
26+
return (x + 1) | 0
27+
})
28+
```
29+
30+
</CodeTab>
31+
32+
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.
33+
34+
### References
35+
36+
- [Use Guaranteed Uncurrying](/docs/manual/latest/bind-to-js-function#solution-use-guaranteed-uncurrying)

0 commit comments

Comments
 (0)