Java if ternary operator and Collections.emptyList()

ε祈祈猫儿з 提交于 2019-12-17 16:13:32

问题


Could you please explain why with the first return type the code can't be compiled? The message is : Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String>.

Is there inserted an explicit cast in the second case ?

public class GenericsTest {

        private String getString() {
            return null;
        }

        public List<String> method() {
            String someVariable = getString();
            //first return type
            //return someVariable == null ? Collections.emptyList() : Collections.singletonList(someVariable);
            //second return type
            if (someVariable == null) {
                return Collections.emptyList();
            } else {
                return Collections.singletonList(someVariable);
            }
        }
    }

回答1:


Because of type inference rules. I don't know why exactly (you should check the JSL, the ternary operator section), but it appears the ternary expression does not infer the type parameter from the return type.

In other words, the type of the ternary expression depends on the types of its operands. But one of the operands has undetermined type parameter (Collections.emptyList()). At that point the ternary expression still does not have a type, so it cannot influence the type parameter. There are two types to be inferred - one is the result of the ternary expression, and the other is the type parameter of the .emptyList() method.

Use Collections.<String>emptyList() to explicitly set the type




回答2:


The type of the expression flag ? trueCase : falseCase is the most common type of the two cases.

In this case the most common type of Collections.emptyList() and Collections.singletonList(someVariable) is List<? extends Object> because it cannot "see in the future" that Collections.emptyList() should return List<String> in the expression.


When you do:

return Collections.emptyList();

the compiler can be smart and detect the type by the return type and check the correctness (inferred).




回答3:


Because Collections.emptyList() doesn't return List<String>. You set explicit the result of the method to List<String> and that means that you have to return a such list.

For example

return Collection<String>.emptyList(); 

or

return new ArrayList<String>();

would work fine.



来源:https://stackoverflow.com/questions/7738938/java-if-ternary-operator-and-collections-emptylist

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