简体   繁体   中英

Java wildcard in multi-level generic type

Why Bar.go is OK with argument f2 but not with argument f1 ?

public class HelloWorld {
    public static void main(String[] args) {
        Foo<Foo<?>> f1 = new Foo<Foo<?>>();
        Foo<Foo<String>> f2 = new Foo<Foo<String>>();
        Bar.go(f1);     // not OK
        Bar.go(f2);     // OK
    }

    public static void p(Object o) {
        System.out.println(o);
    }
}

class Foo<E> {
}

class Bar {
    public static <T> void go(Foo<Foo<T>> f) {
    }
}

Shouldn't the compiler automatically infer type T as capture of ? in both cases?

Foo<Foo<?>> f1 = new Foo<Foo<?>>();

This implies that the type is unknown and objects of any type can be added to Foo<Foo<?>> that are heterogeneous and compiler cannot guarantee that all object in Foo<Foo<?>> are of same type. Hence it cannot be passed to Bar.go that takes a bounded type as parameter.

You can instead declare that as Foo<Foo<Object>> f1 = new Foo<Foo<Object>>(); to pass it to Bar.go where you explicitly mention everything is of type Object .

Great question!

(In the following comments, wrt a class generic in E like Foo< E > , define "covariant method" as a method that returns an E without having any parameters using E , and a "contravariant method" as the opposite: one which takes a formal parameter of type E but doesn't return a type involving E . [The real definition of these terms is more complicated, but never mind that for now.])

It seems that the compiler is trying to bind T to Object in the case of f1 , because if you do

class Bar0 {
    public static < T > void go( Foo< Foo< ? extends T > > f ) {
        // can pass a Foo< T > to a contravariant method of f;
        // can use any result r of any covariant method of f,
        // but can't pass T to any contravariant method of r
    }
}

then the go(f1) works, but now go(f2) doesn't, because even though Foo< String > <: Foo< ? extends String > Foo< String > <: Foo< ? extends String > , that does not imply that Foo< Foo< String > > <: Foo< Foo< ? extends String > > Foo< Foo< String > > <: Foo< Foo< ? extends String > > .

Here are a few modifications that compile for both f1 and f2 :

class Bar1 {
    public static < T > void go( Foo< ? super Foo< T > > f ) {
        // can't properly type the results of any covariant method of f,
        // but we can pass a Foo< T > to any contravariant method of f
    }
}

class Bar2 {
    public static < T > void go( Foo< ? extends Foo< ? extends T > > f ) {
        // can't pass a Foo< T > to a contravariant method of f;
        // can use result r of any covariant method of f;
        // can't pass a T to a contravariant method of r;
        // can use result of covariant method of r
    }
}

A Nice Read What do multi-level wildcards mean?

Example:

Collection< Pair<String,Long> >        c1 = new ArrayList<Pair<String,Long>>();

Collection< Pair<String,Long> >        c2 = c1;  // fine
Collection< Pair<String,?> >           c3 = c1;  // error
Collection< ? extends Pair<String,?> > c4 = c1; // fine  

Of course, we can assign a Collection<Pair<String,Long>> to a Collection<Pair<String,Long>> . There is nothing surprising here.

But we can not assign a Collection<Pair<String,Long>> to a Collection<Pair<String,?>> . The parameterized type Collection<Pair<String,Long>> is a homogenous collection of pairs of a String and a Long ; the parameterized type Collection<Pair<String,?>> is a heterogenous collection of pairs of a String and something of unknown type. The heterogenous Collection<Pair<String,?>> could for instance contain a Pair<String,Date> and that clearly does not belong into a Collection<Pair<String,Long>> . For this reason the assignment is not permitted.

I will prove that if the compiler allows Bar.go(f1); , the type system(safety) would be broken:

Java grammar allows you to use T as a type to declare variables in go() . Something like: T t = <something> .

Now, let's use ArrayList instead of Foo ,

Then we have:

class HW {
 public static void main(String[] args) {
        ArrayList<ArrayList<?>> f1 = new ArrayList<ArrayList<?>>();
        go(f1);     // not OK
    }

    public static <T> void go(ArrayList<ArrayList<T>> f) {
    }
}

ArrayList<?> is supertype of ArrayList<String> , it is also supertype of ArrayList<Integer> , meaning that you can do the following in main :

ArrayList<?> s = new ArrayList<String>();
f1.add(s);

ArrayList<?> i = new ArrayList<Integer>();
f1.add(i);

Now, let's assume that the compiler allows you to call go() with f1 as argument. The option to infer T are:

  1. T = Object , but ArrayList<ArrayList<Object>> is not ArrayList<ArrayList<?>> because ArrayList<Object> is not the same type as ArrayList<?> So that is not allowed option.

  2. T = ? , then we would be able to do:

     public static <T> void go(ArrayList<ArrayList<T>> f) { ArrayList<T> alt1 = f.get(0); // ArrayList<String> T str = alt1.get(0); ArrayList<T> alt2 = f.get(1); // ArrayList<Integer> alt2.add(str); // We have added String to List<Integer> // ... type system broken } 

go() to work in both cases you have to do:

public static void go(ArrayList<? extends ArrayList<?>> f) {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM