public List<String> foo1() {
List<String> retval = bar();
if (retval == null)
return Collections.emptyList();
else
return retval;
}
public List<String> foo2() {
List<String> retval = bar();
return retval == null ? Collections.emptyList() : retval;
}
Why does foo1()
compiles fine whereas foo2()
has an error? (to be more precise "Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String>")
I would have thought that both functions would compile to the same bytecode, so a clever compiler should infer the correct type for emptyList()
...
bar
returning? Can you post the code? And is the error inCollections.emptyList()
orretval
?List<String> bar()
. But I don't think this is very relevant to the error.