In unit testing, you sometimes want a method like this one:
<V> void checkEq(V expectation, V value) {
if (!expectation.equals(value))
throw new IllegalArgumentException("not equal");
}
(assume for a second that we can't use junit for some reason) The idea is that the compiler should ensure both arguments have the same type. However the compiler doesn't complain about this invocation, where the types obviously differ:
checkEq(1, "");
I've tried some variations of this, like checkEq(V expectation, Supplier<V> value) and checkEq(1, () -> ""). It seems like the compiler is always happy to insert Object for V. Is there some way to tell the compiler not to use the trivial Object solution?