Java ternary operator influence on generics type inference

我的梦境 提交于 2019-11-27 22:57:05

问题


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()...


回答1:


Compiles for me fine in java 8.

Earlier versions of Java might need more help

return retval == null ? Collections.<String>emptyList() : retval;

should work.

EDIT This is due to improvements in Java 8 type inference as explained here

http://openjdk.java.net/jeps/101

And here's a blog with the highlights: http://blog.jooq.org/2013/11/25/a-lesser-known-java-8-feature-generalized-target-type-inference/




回答2:


This is related with Type Inference from a generic method.

In case of code before ver. 8. It must be declared the type of result for this case.

return retval == null ? Collections.<String>emptyList() : retval;

Since ver. 8 notion of what is a target type has been expanded to include method arguments. So this is no longer required.



来源:https://stackoverflow.com/questions/24289320/java-ternary-operator-influence-on-generics-type-inference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!