Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why nested wildcard capture is not possible?

I'm struggling to capture a wildcard when it is "nested in another wildcard". Is it possible?

The code:

public class ConvolutedGenerics {

    // listClass is a class implementing a List of some Serializable type
    public void doSomethingWithListOfSerializables(
            Class<? extends List<? extends Serializable>> listClass) {

        // Capture '? extends Serializable' as 'T extends Serializable'
        // The line does not compile with javac 7
        captureTheWildcard(listClass); // <------ zonk here
    }

    // listClass is a class implementing a List of some Serializable type
    private <T extends Serializable>
            void captureTheWildcard(
                    Class<? extends List</* ? extends */T>> listClass) {

        // Do something
    }
}

compiled with javac 7 produces:

ConvolutedGenerics.java:18: error: method captureTheWildcard in class ConvolutedGenerics cannot be applied to given types;
                captureTheWildcard(listClass);
                ^
  required: Class<? extends List<T>>
  found: Class<CAP#1>
  reason: no instance(s) of type variable(s) T exist so that argument type Class<CAP#1> conforms to formal parameter type Class<? extends List<T>>
  where T is a type-variable:
    T extends Serializable declared in method <T>captureTheWildcard(Class<? extends List<T>>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends List<? extends Serializable> from capture of ? extends List<? extends Serializable>
1 error

Besides many more simpler cases I've found

  • Incompatible generic wildcard captures
  • Using Java wildcards

but I could not infer an answer for my problem from those.

like image 321
Piotr Findeisen Avatar asked Oct 21 '22 00:10

Piotr Findeisen


1 Answers

It is not possible, as you probably already know.

Let me illustrate with a counter-example:

List<Integer> foo = Arrays.asList(1,2,3);
List<String> bar = Arrays.asList("hi","mom");
List<List<? extends Serializable>> baz = Arrays.asList(foo, bar);
doSomethingWithListOfSerializables(baz);

public void doSomethingWithListOfSerializables(
        List<? extends List<? extends Serializable>> listList) {

    captureTheWildcard(listList); 
}

private <T extends Serializable>
        void captureTheWildcard(
                List<? extends List<T>> listList) {

    // Do something
}

What should T be?

like image 83
newacct Avatar answered Oct 28 '22 23:10

newacct