Let's say I have a function with the following signature:
<T> T f(List<T> a, T b)
Obviously, I can do the following:
List<Integer> list = new ArrayList<Integer>();
int x = 0;
int y = f(list, x);
However, I am not allowed to do this:
List<List<?>> list = new ArrayList<List<?>>();
List<Integer> x = new ArrayList<Integer>();
List<Integer> y = f(list, x);
In that case, what I would want is a signature something like this:
<T, V> T<V> foo(List<T<?>>, T<V> x)
However, there is two problems with this. First of all, Java simply doesn't let me do that, citing Type 'T' does not have type parameters
. Secondly, I assume the first scenario I described as possible would no longer be possible.
So, my question is: is there any way to write a function with a signature that allows for both of the above scenarios?
<T> T f(List<? super T> a, T b)