You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _blogposts/2023-09-18-uncurried-mode.mdx
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,7 @@ This comes at a price though. Here are some examples to show the drawbacks of cu
32
32
33
33
- Errors because of changed function signatures have their impact at the use site. Consider this example, where the signature of the onChange function is extended with
34
34
a labeled argument
35
+
35
36
```diff
36
37
@react.component
37
38
- let make = (~onChange: string => option<unit => unit>) => {
@@ -45,6 +46,7 @@ This comes at a price though. Here are some examples to show the drawbacks of cu
45
46
})
46
47
}
47
48
```
49
+
48
50
- If you wanted explicitly uncurry a function, you needed to annotate it with the uncurried dot.
49
51
```rescript
50
52
(. param) => ()
@@ -53,25 +55,29 @@ This comes at a price though. Here are some examples to show the drawbacks of cu
53
55
- In the standard library (`Belt`), there are both curried and uncurried versions of the same function so you were required to think for yourself when to use the uncurried version and when only the curried one will work.
54
56
- In combination with `ignore` / `let _ = ...`, curried can lead to unexpected behavior at runtime after adding a parameter to a function, because you are accidentally ignoring the result of a partial evaluation so that the function is not called at all.
55
57
1. Have a look at this simple function. It is assigned to `_` because we ignore the resulting `string` value.
58
+
56
59
```res
57
60
let myCurriedFn = (~first) => first
58
61
let _ = myCurriedFn(~first="Hello!")
59
62
// ^ string
60
63
```
61
64
62
65
2. Now the function got a second parameter `~second`. Here, the resulting value is a function, which means it is not fully applied and thus never executed.
66
+
63
67
```res
64
68
let myCurriedFn = (~first, ~second) => first ++ " " ++ second
65
69
let _ = myCurriedFn(~first="Hello!")
66
70
// ^ (~second: string) => string
67
71
```
68
72
69
73
3. One way to prevent such errors is to annotate the underscore with the function's return type:
74
+
70
75
```res
71
76
let _: string = myCurriedFn(~first="Hello!")
72
77
```
73
78
74
79
4. However, the same issue arises when using the built-in ignore function, which cannot be annotated:
0 commit comments