Ivan
Ivan

Reputation: 847

Java wrapped generics compilation error

I have a very basic code to reproduce the issue:

public class Main {
  public static <T extends Number> void bar(Wrapper<List<T>> wrapper) {
    // do something
  }

  public static void foo(Wrapper<List<? extends Number>> wrapper) {
    // do something
  }

  public static <T extends Number> void bar2(List<T> aList) {
    // do something
  }

  public static void foo2(List<? extends Number> aList) {
    // do something
  }

  public static void main(String[] args) {
    List<Integer> aList = Collections.singletonList(1);
    foo2(aList); // compiles
    bar2(aList); // compiles

    Wrapper<List<Integer>> wrapper = new Wrapper<>(Collections.singletonList(1));
    foo(wrapper); // not compiles
    bar(wrapper); // compiles
  }

  private static class Wrapper<T> {
    private final T t;

    public Wrapper(T t) {
      this.t = t;
    }
  }
}

So the question is why javac gives an error when I try to compile the code:

Main.java:26: error: method foo in class Main cannot be applied to given types;
    foo(wrapper); // not compiles
    ^
  required: Wrapper<List<? extends Number>>
  found: Wrapper<List<Integer>>
  reason: argument mismatch; Wrapper<List<Integer>> cannot be converted to Wrapper<List<? extends Number>>
1 error

Upvotes: 1

Views: 100

Answers (3)

Andy Turner
Andy Turner

Reputation: 140504

Wrapper<List<Integer>> is not a subtype of Wrapper<List<? extends Number>>.

Use Wrapper<? extends List<? extends Number>>.


To see why you can't pass Wrapper<List<Integer>> to foo, consider the following: I've replaced Wrapper with List, but it's the same thing from a type safety point of view.

List<List<? extends Number>> list = new ArrayList<>();
List<List<Integer>> intList = new ArrayList<>();

So, you can add something to list:

List<Double> doubles = new ArrayList<>(Arrays.asList(0.0));
list.add(doubles);

But, if you were able to write this assignment:

list = intList;

Then calling list.add(doubles) would mean there would then be a List<Double> in intList. This is not type safe, and thus forbidden.

Ideone demo

However, if list has type List<? extend List<? extends Number>>, then you can't invoke add on it, so you can't get into this situation, so that would be safe.

Upvotes: 2

c0der
c0der

Reputation: 18792

Because List<Integer> is not a subclass of List<? extends Number>

Upvotes: 1

Dmitry Gorkovets
Dmitry Gorkovets

Reputation: 2276

Because Java generics are not covariant.

In your case

Wrapper<List<Integer>>

instance cannot be used as

Wrapper<List<? extends Number>>

parameter because

List<Integer>

does not extend

List<? extends Number>

Upvotes: 2

Related Questions