Re:A new approach to error handling
by Yoric on Tuesday July 30, 2024 @04:11PM
(#41113635)
I assume that the poster meant that Java could have had `Result<T, E>` return values like Rust if it had launched with parametric polymorphism, instead of having a layer of the type system dedicated to errors.
[ Parent
| on HN ]
It still can have it, see e.g. Vavr, or other functional libraries.
Edit: Not sure why the downvote.
This works in Java today with proper pattern matching (I left out the complete name from the permit clause, it won’t compile as is):
sealed interface <T,E> Result permits Ok, Error {
record Ok(T val) implements Result<T,E> {}
record Error(E err) implements Result<T,E> {}
}Man I hope Vavr survives. It is an oasis of rationality and I'm disheartened that https://docs.vavr.io/ appears to be down. Its Set implementation actually has union and intersection (!!!)