Is there any proposal to add anything analogous to OCaml's `function` keyword or Haskell's `LambdaCase` extension?
In there
let optionBoolToBool = opt => {
switch opt {
| Some(trueOrFalse) => trueOrFalse
| None => false
}
}
seems redundant. Since there is no need for reference to opt.
We used to have this in reason syntax with fun keyword.
The reason behind removing this was reckless currying. @IwanKaramazow can explain better.
I'm also missing this as i write more code in rescript but at the same time I don't like the fun syntax in reason.
I was thinking about something like scala's case match expression.
val f = {
case Some(b) => b
case None => false
}
so in rescript it would look something like this
let f = {
| Some(b) => b
| None => false
}
or
let f = switch {
| Some(b) => b
| None => false
}
but they are ambigious and may be hard to parse.
it may be good to think about it, there's also an issue when converting from ml/reason which can be solved if we introduce this feature #147 .
I was curious if it's possible to repurpose switch for this lately. Hopefully, it could also work inline. Because sometimes instead of this:
switch
value
->someOperation(a)
->someOtherOperation(b, c)
{
| Some(x) => ...
| None => ...
}
You really want this:
value
->someOperation(a)
->someOtherOperation(b, c)
->switch {
| Some(x) => ...
| None => ...
}
You could also consider adding an underscore lambda function:
let optionBoolToBool = switch _ {
| Some(trueOrFalse) => trueOrFalse
| None => false
}
Also useful for piped functions:
value
->someOperation(a)
->someOtherOperation(b, c)
->switch _ {
| Some(x) => ...
| None => ...
}
I like the switch _ syntax.
I like the
switch _syntax.
Me too. It does not need an extra keyword and in contrast to the old fun keyword, the _ makes it clearer that there must be some shorthand magic going on.
The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.