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