JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Incorrect variance in `hasItem` matcher

Open CarstenLeue opened this issue 7 months ago • 3 comments

The hasItem matcher has the signature

Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher)

but it should be

Matcher<Iterable<? extends T>> hasItem(Matcher<? super T> itemMatcher)

because the matcher needs to read items from the Iterable and it needs the contract that the sequence contains at least items of type T, so it should be covariant in T

The current implementation states the opposite, it would be applicable it the matcher wanted to write to the iterable, but that is (a) not possible, since there is no write operation on an iterable and (b) not its usecase.

CarstenLeue avatar Jul 16 '25 12:07 CarstenLeue