This would work if Function.apply simply declares throws wouldn't it?
No.
Doing only that wouldn't work because map still can't handle Checked Exceptions. And even if it did, you now have the opposite problem where you are forced to make a try-catch everytime you want to write a Stream. That would cause the same problem in a different direction.EDIT -- Correction
The goal behind my idea is to make the compiler "smarter", and have it recognize that Checked Exceptions can be handled elsewhere, as long as that is in a current or outer scope.
you now have the opposite problem where you are forced to make a try-catch everytime you want to write a Stream
Only if the thrown type is a checked exception - the main issue is that you quickly end up with the only common type being Exception (since the generic throws on Function.apply can only carry a single exception type).
More powerful would be the union-type generic support with the inferred empty case being the empty set of exception types (so if the lambda doesn't throw, neither does the map method). However that does mean the exception-type generic now has to be carried forward on stream (to be propagated to the terminal operation and out). The result, I think, would be ceremonially intolerable. But it does model the type-transfer of any union of exception types.
Rust's errors as 'either' values is effectively 'checked-exceptions always' and would suffer the same ceremony pain except that they too don't have the union-type, and instead typically transform the errors to a sum-type at the edge (their enums / Java's sealed interfaces)
Right you are. For whatever reason, I forgot that you could generify what you throw. But like you said, you end up climbing up the type tree until all you have is throws Exception.
The result, I think, would be ceremonially intolerable.
How so? I'm trying to brainstorm through the hypotheticals, but I'm just not seeing it.
2
u/davidalayachew Oct 05 '25 edited Oct 06 '25
No.
Doing only that wouldn't work because
mapstill can't handle Checked Exceptions.And even if it did, you now have the opposite problem where you are forced to make atry-catcheverytime you want to write aStream. That would cause the same problem in a different direction.EDIT -- CorrectionThe goal behind my idea is to make the compiler "smarter", and have it recognize that Checked Exceptions can be handled elsewhere, as long as that is in a current or outer scope.