java - Why does this generic code compile? -
javac infers t string in method f(). rules in language spec lead conclusion?
<t> t g(){ return null; } string f() { return g(); }
i suspect compiler using section 15.12.2.8:
if method result occurs in context subject assignment conversion (§5.2) type s, let r declared result type of method
this treating return statement if subject assignment conversion. example, imagine converted f() to:
string f() { string tmp = g(); return tmp; } now whether is subject assignment conversion, section 14.17 (the return statement) contains this:
a return statement expression must contained in method declaration declared return value (§8.4) or compile-time error occurs. expression must denote variable or value of type t, or compile-time error occurs. type t must assignable (§5.2) declared result type of method, or compile-time error occurs.
that reference 5.2 "assignment conversion" section, guess means expression g() is "subject assignment conversion", leading part of section 15.12.2.8 being applicable.
Comments
Post a Comment