Note that there are some explanatory texts on larger screens.

plurals
  1. POJava multiple generic collection parameters compile error
    primarykey
    data
    text
    <p>So strange! Please have a look the code first:</p> <pre><code>public class A {} public class B extends A {} public class C extends A {} public class TestMain { public &lt;T extends A&gt; void test(T a, T b) {} public &lt;T extends A&gt; void test(List&lt;T&gt; a, List&lt;T&gt; b) {} public void test1(List&lt;? extends A&gt; a, List&lt;? extends A&gt; b) {} public static void main(String[] args) { new TestMain().test(new B(), new C()); new TestMain().test(new ArrayList&lt;C&gt;(), new ArrayList&lt;C&gt;()); new TestMain().test(new ArrayList&lt;B&gt;(), new ArrayList&lt;C&gt;()); new TestMain().test1(new ArrayList&lt;B&gt;(), new ArrayList&lt;C&gt;()); } } </code></pre> <p>The statement <code>new TestMain().test(new ArrayList&lt;B&gt;(), new ArrayList&lt;C&gt;())</code> gets a compilation error:</p> <blockquote> <p>Bound mismatch: The generic method test(T, T) of type TestMain is not applicable for the arguments <code>(ArrayList&lt;B&gt;, ArrayList&lt;C&gt;)</code>. The inferred type <code>ArrayList&lt;? extends A&gt;</code> is not a valid substitute for the bounded parameter <code>&lt;T extends A&gt;</code></p> </blockquote> <p>However:</p> <pre><code> new TestMain().test(new B(), new C()) --&gt; compiled ok new TestMain().test(new ArrayList&lt;C&gt;(), new ArrayList&lt;C&gt;()) --&gt; compiled ok new TestMain().test1(new ArrayList&lt;B&gt;(), new ArrayList&lt;C&gt;()) --&gt; compiled ok </code></pre> <p>If we define the generic before the method name, it seems the type of the second generic <code>List</code> parameter must be the same as that of the first. But there is no restriction if we define generic in parameters.</p> <p>Is it a feature or a bug of the compile program? Is there some documentation about it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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