I came across a problem with generics which keeps me puzzled of how the compiler actually deals with generic types. Consider the following:
// simple interface to make it a MCVE
static interface A<F, S> {
public F getF();
public S getS();
}
static <V, S> Comparator<A<V, S>> wrap(Comparator<S> c) {
return (L, R) -> c.compare(L.getS(), R.getS());
}
The following will not compile because both generic types are reduced to Object when calling thenComparing:
Comparator<A<String, Integer>> c = wrap((L, R) -> Integer.compare(L, R))
.thenComparing(wrap((L, R) -> Integer.compare(L, R)));
But if I break them up like the following example, everything compiles (and runs) correctly:
Comparator<A<String, Integer>> c = wrap((L, R) -> Integer.compare(L, R));
c = c.thenComparing(wrap((L, R) -> Integer.compare(L, R)));
So the question is: what happens here? I suspect this is due to some weird behavior of the compiler rather than intended language specification? Or am I missing something obvious here?