3 ms·
stream.map(f).collect(…) is where checked exceptions failed. #map is a generic method that should obviously throw each of the checked exceptions f can throw, b
by erik_seaberg 7y ago
stream.map(f).collect(…)
is where checked exceptions failed. #map is a generic method that should obviously throw each of the checked exceptions f can throw, but without some kind of generic sum type Java can't express that. Everyone is forced to choose between "can throw any exception" (e.g., Callable<T>) and "can only throw unchecked exceptions" (e.g., Function<T, U>), each of which spreads virally and punishes anyone using specific throws clauses.
- lokedhs 7y agoThere is no need to get rid of checked exceptions to fix that. You need generics for exceptions. If the inner function can throw some exception, then it should be possible to declare the stream method that it will throw the same exception. I believe that the JVM doesn't actually support this, so the reasons were got the horrible situation we're in is because they didn't want to sacrifice backward compatibility.
- erik_seaberg 7y agoI think we're saying the same thing. If f(T) can return U or throw A or B or C, there needs to be a way to declare that Stream<T>#map takes a Function<T, U, A|B|C> and returns a Stream<U> or throws A or B or C. But that sum type A|B|C doesn't exist in Java if there's no common base class.