jocarreira / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Wrong signature for allOf/anyOf #28

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
<T> Matcher<T> allOf(Matcher<? extends T>... matchers)

Shouldn't it be

<T> Matcher<T> allOf(Matcher<? super T>... matchers)

instead?

Consider the following hierarchy T, T1 extends T, T2 extends T, T3 extends
T. Say T1Matcher is a matcher for T1 and T2Matcher is a matcher for T2.
Now, from the first signature, allOf(t1Matcher, t2Matcher) -> tMatcher, is
a matcher for T. But tMatcher.matches(t3) should fail, because none of
t1Matcher nor t2Matcher knows how to cope with t3 instances. I think this
error is not caught by the type system because matches expect an object
instead of a T, but it surely will be caught at runtime.

Solution: change signature as suggested. Also, according to a previous
issue posted by me, provide alternative signatures:

<T> Matcher<T> allOf(Matcher<? super T> matcher1, Matcher<? super T> matcher2)

<T> Matcher<T> allOf(Matcher<? super T> matcher1, Matcher<? super T>
matcher2, Matcher<? super T> matcher3)

in order to avoid the unchecked cast warnings from the java compiler.

Original issue reported on code.google.com by carlosjo...@gmail.com on 1 Jan 2008 at 8:09

GoogleCodeExporter commented 8 years ago
Already implemented

Original comment by nat.pr...@gmail.com on 1 Jan 2008 at 8:28

GoogleCodeExporter commented 8 years ago

Original comment by smgfree...@gmail.com on 22 Nov 2008 at 12:19