Example A
Study the following snippet:
public class ExampleA {
static class Pair<F,S> { }
static <F,S> Pair<F,S> anyPair() { return null; }
static <F,S> void process(Pair<F,S> p1, Pair<F,S> p2) { return; }
public static void main(String[] args) {
Pair<String,Integer> p = anyPair();
process(p, anyPair()); // doesn't compile
}
}
Can someone explain why type inference works for the assignment to local variable p but not for the second actual parameter to process?
Example B
This is perhaps simpler to understand:
public class ExampleB {
public static <E> void process(Set<E> s1, Set<E> s2) { return; }
public static void main(String[] args) {
process(new HashSet<String>(), Collections.emptySet()); // doesn't compile
}
}
Same question: why doesn't it compile?
I'd hope that Collections.emptySet() would just work for ANY parameterized Set type.