The following statement compiles:
hasFeature(Objects::toString, equalTo(1));
Looking at the current factory method signature:
<T, U> Matcher<T> hasFeature(Function<T, U> featureFunction, Matcher<? super U> featureMatcher)
We can see that U is being inferred as Object which is the common supertype of String and Integer. This is explained by Why doesn't type argument inference fail when I provide inconsistent method arguments? and is due to Java 8's Generalized Target-Type Inference.
To raise a compilation error requires explicit type parameters:
hasFeature((Function<Object, String>) Objects::toString, equalTo(1)); // error
ComposeMatchers.<Object, String>hasFeature(Objects::toString, equalTo(1)); // error
But this is unwieldy.
The following statement compiles:
Looking at the current factory method signature:
We can see that
Uis being inferred asObjectwhich is the common supertype ofStringandInteger. This is explained by Why doesn't type argument inference fail when I provide inconsistent method arguments? and is due to Java 8's Generalized Target-Type Inference.To raise a compilation error requires explicit type parameters:
But this is unwieldy.