Given this generic function:
<T> List<T> function() { return null; }
Why does this compile
List<String> l = function();
While this does not?
List<String> l = (List<String>) function();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It looks like type inference happens after the cast is attempted (by the compiler).
The type of the expression is determined by the left side of the expression, which is not yet “parsed” when the cast is attempted. And the cast itself fails because, with not-yet-inferred type, the result (of the method invocation) is of type
List<Object>Section 15.12.2.7 of the JLS indicates that type inference happens last.