Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn generic type from generic function
    text
    copied!<p>we have a method more or less like the following. however we currently return List which in function bla() would return <code>List&lt;Bar&gt;</code> at runtime.</p> <p>I'm looking for a way to make both</p> <pre><code>List&lt;Interface&gt; = troubleFuction(foo, bar.getCLass());; </code></pre> <p>and</p> <pre><code>List&lt;Bar&gt; = troubleFuction(foo, bar.getCLass());; </code></pre> <p>possible. basicaly i want it to return List which would be compatible with interface however this gives the following error</p> <p>*Type mismatch: cannot convert from <code>List&lt;capture#3-of ? extends Bar&gt;</code> to <code>List&lt;Interface&gt;*</code> </p> <p>is there any way to make this return type possible or does runtime erasure make this impossible</p> <pre><code>public &lt;T1 extends Interface, T2 extends Interface&gt; List&lt;"problem"&gt; troubleFunction( T1 in, Class&lt;T2&gt; clazz) { return in.doStuffWhichGeneratesAlistOF(clazz) } public void bla() { Foo foo = new Foo(); // implements interface Bar bar = new Bar(); // implements interface List&lt;Interface&gt; ifaces = toubleFuction(foo, bar.getCLass()); List&lt;Bar&gt; mustAlsoWork = toubleFuction(foo, bar.getCLass()); } </code></pre> <p>edit: in a lot of the existing code base the method is called like</p> <pre><code>List&lt;Bar&gt; result = troubleFunction(List&lt;Interface&gt; list, Bar.class); </code></pre> <p>thus this return type must stay compatible (rewrite/re-factor is not an option)</p> <p>essentially i want the method to return List<code>&lt;? super Bar&gt;</code> if called as</p> <pre><code>troublefunction(foo, Bar.class); </code></pre> <p>and <code>List&lt;? super Foo&gt;</code> when called as</p> <pre><code>troublefunction(foo, Bar.class); </code></pre>
 

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