Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Exceptions done well should outperform return value checking. However it’s very difficult to make it perform well and for some reason people prefer -> Result<T, E> instead of -> T throws E which is basically the same thing.


The difference is the monadic capabilities of the result type. Thrown exceptions pepper codebases in ways that are even more unfortunate than monadic composition, which is already kind of iffy, but at least has generic transforms for error recoveries, turning errors to successes of a different type and so on. You end up with far less boilerplate.


Well, PLs with effect systems are basically meant to solve exactly this problem.

E.g. a generic `map` function can become throwing if it handles throwing lambdas, but are otherwise non-throwing - this is pretty much the main pain point with Java's checked exceptions.


I’m not so sure. The page goes over how it works in Scala 3 and it’s a little bit cleaner. But there is some nicety in handling return and exception uniformly in some cases.


They're not the same since exceptions typically require non-local control-flow, where Result types require local control flow.


Basically the same thing except the former is pessimistic and the latter optimistic.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: