Say, I have a method:
public static <T> Collection<T> addToCollection(T element, Collection<T> collection) {
collection.add(element);
return collection;
}
And then when trying to compile this code:
Integer i = 42;
Collection<Integer> result = addToCollection(i, Collections.emptyList());
I get an error Type mismatch: cannot convert from Collection<Object> to Collection<Integer>
.
Could anyone explain why the type system cannot infer that Collections.emptyList() should be of type Collection<Integer>
?
The example above is obviously quite artificial, but I stumble upon that limitation all the time and it's really annoying. After having read Effective Java I have found out that you can simply do Collections.<Integer>emptyList()
(must say, that was quite a revelation for me at the time) and have everything compiling smoothly, but when you have some complicated type then it really is a nuisance.
I'm just wondering if this is some sort of bug, or are there any valid reasons for it to work that way?