4 ms·
No, but you can have an interface like interface Func<P, R, X extends Exception> { R func(P param) throws X; } or the same with more than
by layer8 2y ago
No, but you can have an interface like
interface Func<P, R, X extends Exception>
{
R func(P param) throws X;
}
or the same with more than one exception type, and convert your lambda to that. This works. The only problem is that you can’t abstract over an arbitrary number of exception types.
In principle, one could imagine a syntax for variadic type parameters like
interface Func<P..., R, X... extends Exception>
{
R func(P... params) throws X...;
}
that would solve that problem.
- erik_seaberg 2y agoWill the compiler infer that a lambda or a method ref implements Func with its exception type param, or do you have to rewrite call sites?
- layer8 2y agoThe compiler already infers that in current Java for one checked exception type, and also for several exception types at least in some cases (the latter seems to be a little more buggy in the current implementation).