5 ms·
Option types require do-notation / computation expressions to be practical in my view. Otherwise you get the pyramid of doom! match x with | Some y ->
by de_keyboard 5y ago
Option types require do-notation / computation expressions to be practical in my view. Otherwise you get the pyramid of doom!
match x with
| Some y ->
match y.Foo with
| Some z ->
match z.Bar with
| Some w -> printfn $"{w.Qux}"
| None -> ()
| None -> ()
| None -> ()
Compared to...
option {
let! y = x
let! z = y.Foo
let! w = z.Bar
printfn $"{w.Qux}"
}
Funny quirk in Java, the compiler checks that calls to `opt.get()` occur inside an `opt.isPresent()` branch!
if (opt.isPresent()) {
println(opt.get());
}
- Zababa 5y agoThe pyramid of doom means that you're explicit about error handling at each step though, which doesn't look easy with the do notation.
- de_keyboard 5y agoYou must understand the semantics of the monad you are operating in, but both cases are still handled in the do-notation version.