Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit</strong>: This answer is wrong. Take a look at accepted answer.</p> <p>I think the issue comes down this: compiler does not see the type of f2 (i.e. Field) and the inferred type of formal parameter (i.e. Field -> Field) as the same type.</p> <p>In other words, it looks like type of f2 (Field) is considered to be a subtype of the type of formal parameter Field (Field). Since Field is at the same type a subtype of Object, compiler cannot pick one method over another.</p> <p><strong>Edit</strong>: Let me expand my statement a bit</p> <p>Both methods are <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.1" rel="nofollow">applicable</a> and it looks like the <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.2" rel="nofollow">Phase 1: Identify Matching Arity Methods Applicable by Subtyping</a> is used to decide which method to call and than rules from <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#301183" rel="nofollow">Choosing the Most Specific Method</a> applied, but failed for some reason to pick second method over first one.</p> <p><em>Phase 1</em> section uses this notation: <code>X &lt;: S</code> (X is subtype of S). Based on my understanding of <code>&lt;:</code>, <code>X &lt;: X</code> is a valid expression, i.e. the <code>&lt;:</code> is not strict and includes the type itself (X is subtype of X) in this context. This explains the result of Phase 1: both methods are picked as candidates, since <code>Field&lt;Object&gt; &lt;: Object</code> and <code>Field&lt;Object&gt; &lt;: Field&lt;Object&gt;</code>.</p> <p><em>Choosing the Most Specific Method</em> section uses same notation to say that one method is more specific than another. The interesting part the paragraph that starts with "One fixed-arity member method named m is more specific than another member...". It has, among other things:</p> <blockquote> <p>For all j from 1 to n, Tj &lt;: Sj.</p> </blockquote> <p>This makes me think that in our case second method <strong>must</strong> be chosen over the first one, because following holds:</p> <ul> <li><code>Parameter&lt;Object&gt; &lt;: Parameter&lt;Object&gt;</code></li> <li><code>Field&lt;Object&gt; &lt;: Object</code></li> </ul> <p>while the other way around does not hold due to <code>Object &lt;: Field&lt;Object&gt;</code> being false (Object is not a subtype of Field).</p> <p>Note: In case of String examples, Phase 1 will simply pick the only method applicable: the second one.</p> <p>So, to answer your questions: I think this is a bug in compiler implementation. Eclipse has it is own incremental compiler which does not have this bug it seems.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload