I created a "producer" interface (to be used with method references, respectively to be easily mocked for unit tests):
@FunctionalInterface
public interface Factory<R, T, X extends Throwable> {
public R newInstanceFor(T t) throws X;
}
which I created like that, as my first use case actually had to throw some checked WhateverException
.
But my second use case doesn't have an X to throw.
The best I could come up with to make the compiler happy is:
Factory<SomeResultClass, SomeParameterClass, RuntimeException> factory;
That compiles, and does what I need, but still ugly. Is there a way to keep that single interface, but not provide an X when declaring specific instances?
Factory<SomeResultClass, SomeParameterClass, ?>
? – Dekow<?>
is aThrowable
and is treated as such by compiler, forcing you to make a try-catch block. – Delriojava.util.function.Function<T,R>
, which is aP1 => P2
function, notP2 => P1
like yours. – Delrio@FunctionalInterface public interface Factory<R, T> { R newInstanceFor(T t) throws Throwable; }
? – Azo