3 ms·
You can do something conditionally with the monadic types too; you branch/match on them.
by spoiler 2y ago
You can do something conditionally with the monadic types too; you branch/match on them.
- zarzavat 2y agoYes, but what I’m saying is that you can also handle monadic types using try/catch syntax. Consider this imaginary language, the bastard child of Rust and Swift: fn returnsAResult() -> Result<i8, Error> { return rand(0,1) ? Ok(4) : Err(BadLuckError) } do { let x = try returnsAResult() } catch e { print(e) } This language doesn’t exist, but it could. Results are in-band errors where the error is a normal return value. Swift by contrast uses out-of-band errors. Try/catch is one particular syntax for detecting errors. Match syntax is another. In principle there’s nothing preventing match syntax being used with out-of-band errors, even with Java-like exceptions. It would look something like this: match (iCanThrow()) { case 42 -> … catch e -> … _ -> … } I like this one less than the first one though.