If there’s enough interest, this can probably be revisited to get to a complete design for evaluation.
It probably needs a champion to bring it forward.
I think it makes unwrapping values easier. More complex example from rescript-lang/syntax#12:
if let Some(x) = optVal {
doSomethingWith(x)
} else if let Theme({color: Red | Blue, emoji}) = getTheme() {
doSomething(emoji)
} else {
doSomethingElse()
}
Looks more familiar to read as a JS developer than:
ah I see. though the similarity to javascript seems pretty pale to me.
You have switches in js as well right, and are still doing pattern matching the same way
and else if let is a lot more noise than (_, ...
Anyway I am really starting to appreciate haskells “avoid success at all costs” and wish for Rescript to stay as simple as reasonably possible. and one way to do a thing is a good number.
I don’t have an opinion on if let, but I personally really agree with this. Having as few ways as possible to do something is a big benefit in the long run.
if let Some(x) = o.a && Some(y) == x.b {
if let Some(z) == y.c {
z
} else {
1
}
} else {
0
}
In general, those are the kind of properties I would look for: it should give clear advantages for nested cases, and have no additional runtime cost compared to the explicit switches.
You could get rid of the nested switch hell by using utility functions & the -> (pipe) operator:
open Belt
for lnum in 1 to Array.length(lines) {
let someline = lines[lnum - 1]
switch someline
->Option.flatMap(firstChar->Js.Re.exec_)
->Option.flatMap(result => Js.Re.captures(result)[0])
->Option.flatMap(Js.Nullable.toOption) {
| Some("#") => Js.log("branch1")
| Some(":") => Js.log("branch2")
| Some("[") => Js.log("branch3")
| _ => Js.log("something else")
}
}
Functional programming requires a different mindset to problem solving without letting imperative thinking get in the way. I recommend a lot of practice