Reputation: 1147
I have a ThrowingFunction<T, R, E extends Exception>
functional interface with a single abstract method having this signature : R apply(T t) throws E
.
I have a static utility method inside the functional interface that permits users converting ThrowingFunction<T, R, E extends Exception>
instances into Function<T, R>
instances :
static <T, R> Function<T, R> unchecked (final ThrowingFunction<T, R, ?> tf)
{
// code here
}
The problem here is why the type parameters T
and R
don't get inferred by the compiler if I pass the method reference URL::new
into the unchecked
static utility method ?
final URL url = unchecked(URL::new).apply("http://www.examples.com");
I mean, isn't URL::new
a valid condidate for ThrowingFunction<String, URL, MalformedURLException>
and therefore T
and R
should be inferred as String
and URL
respectively ?
Upvotes: 4
Views: 3348
Reputation: 57114
I do not know what the issue is but the answer is yes
, it is a valid candidate and it works if you split it into two statements:
ThrowingFunction<String, URL, MalformedURLException> ctor = URL::new;
final URL url = unchecked(ctor).apply("http://www.examples.com");
Upvotes: 2
Reputation: 120848
You are right, it should work, it most probably has to do with the fact that you are using a method reference and the compiler is somehow getting confused, there are two solutions:
// a lambda
ThrowingFunction.unchecked((String x) -> new URL(x));
// an explicit cast
ThrowingFunction.unchecked((ThrowingFunction<String, URL, MalformedURLException>)URL::new);
Upvotes: 3