Skip to content

hasFeature allows incompatible matcher type for feature type #2

@markhobson

Description

@markhobson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions