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
Support optional named arguments without a final unit in uncurried functions. (#5907)
* Explore default arguments in uncurried functions without a final unit argument
Functions such as this one
```res
let foo1 = (~x=3, ~y) => x+y
```
normally require a final unit unlabeled argument to indicate that the optional argument `x` is not passed.
In this example, writing
```res
let r1 = foo1(~y=11)
```
makes `r1` a function that is still expecting the `x` argument.
Requiring a final unit argument, and passing it as `foo1(~y=11, ())` is the only way to omit `x` in practice.
With uncurried functions, there's the opportunity to treat `foo1(~y=11)` in a special way, meaning that argument `x` is omitted.
TODO:
- Figure out what to do with the warning ""This optional parameter in final position will, in practice, not be optional"
- Figure out the case of all optional arguments. There's no way to pass zero arguments. One could interpret `foo()`, which actually passes `()` as single unlabelled argument, in a special way when `foo` does not accept unlabelled arguments, and use it to mean zero arguments passed.
* Disable Unerasable_optional_argument for uncurried functions via the type checker.
In reality this disables also the check for the body, but this check is on the way out so this should be reasonable in practice without over-complicating the error logic.
* Treat `foo(. )` as empty application if all arguments are optional.
When all the processed arguments to the function are ignored (hence optional), and no arguments are ignored (so no mandatory labelled), it means the uncurried function only has optional arguments.
- If the uncurried type of the function had an unlabelled arg, then it would be caught by the unit application, so it would be a non-ignored argument. But this is not possible as all the argiments are ignored.
- If it had a labelled mandatory argument, then either it would be non-ignored, or omitted, but both cases are excluded.
- It follows that the type only has optional arguments, and that the unit argument was the only argument supplied.
The new mechanism does not kick in when some legit argument is passed alongside the unit. And noes not interfere with cases where the function expects a legitimate unit argument.
* Update CHANGELOG.md
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ subset of the arguments, and return a curried type with the remaining ones https
24
24
- Add support for default arguments in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5835
25
25
- Inline uncurried application when it is safe https://github.com/rescript-lang/rescript-compiler/pull/5847
26
26
- Add support for toplevel `await`https://github.com/rescript-lang/rescript-compiler/pull/5940
27
+
- Support optional named arguments without a final unit in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5907
0 commit comments