3 ms·
> Let exceptions propagate, catching them at as high a level as possible. This can't be done cleanly in Java. Checked exception can encode union types, but th
by ackfoobar 2y ago
> Let exceptions propagate, catching them at as high a level as possible.
This can't be done cleanly in Java.
Checked exception can encode union types, but this extra power is not complemented anywhere else in Java's type system.
E.g. in a `Consumer` lambda passed to `forEach`, Java's checked exception forces you to convert that to a RuntimeException.
- PhilipRoman 2y agoI'd say it is due to a lack of variadic type parameters. You can write something like this: <T, X extends Throwable> void forEach(ThrowingConsumer<T, X> f) throws X; But the only way to have multiple exception types without losing static type checking is to have multiple X parameters, like X1, X2, X3... (with unused parameters being set to some subtype of RuntimeException so that they do not participate in checked exception handling). Whether or not it is worth to write this madness just to satisfy one's OCD is up to the reader.
- ackfoobar 2y ago> it is due to a lack of variadic type parameters To be pedantic, it is due to union types "not complemented anywhere else in Java's type system". Adding variadic type params is a way to solve this. Another way is, of course, to support union types. > with unused parameters being set to some subtype of RuntimeException Or the `Nothing` type (`never` in TypeScript), where `A | Nothing = A`.