4 ms·
They are not quite orthogonal but at least independent. Maybe/Either/Result is a way to represent partial computations at runtime. try/catch is a syntax to de
by zarzavat 2y ago
They are not quite orthogonal but at least independent.
Maybe/Either/Result is a way to represent partial computations at runtime.
try/catch is a syntax to detect partial computations and to do something conditionally if one occurs.
You could have a language that used try/catch syntax to catch Result types, indeed Rust does something like this with the try operator although it’s only try/catch if you squint very hard.
- spoiler 2y agoYou 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.