Casting bounded wildcard to unbounded wildcard within a generic type is an error (X<Y<? extends T>> to X<Y<?>>

davidbak

Consider this sample:

private <T> void m(Class<? extends T> k, Set<Class<? extends T>> sk) {
    Class<?> ku = k;
    Set<Class<?>> sku = sk; // <-- Type mismatch: cannot convert from
                            //     Set<Class<? extends T>> to Set<Class<?>>
}

In other words, I can assign a Class<? extends T> to a Class<?> for some arbitrary T but not a Set<Class<? extends T>> to a Set<Class<?>>.

It probably has something to do with some limitation on covariance/contravariance, but what?

I could introduce a cast: Class.class::cast would do it. But is there a way to bend the compiler to my will with subtle type-fu rather than bashing it in the head with a cast?

rgettman

Even though a Class<? extends T> is a Class<?>, a Set<Class<? extends T>> is not a Set<Class<?>>, for the same reason that even though a Dog is an Animal, a List<Dog> is not a List<Animal>. Here, ? extends T has the same role as Dog, and ? has the same role as Animal.

You'll need a ? extends in front of Class to get this to compile correctly.

Set<? extends Class<?>> sku = sk;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

From Dev

Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

From Dev

Generic bounded wildcard of function input type

From Dev

Java generic upper bounded wildcard error

From Dev

generic wildcard is accepted but not Type T

From Dev

Type parameter vs unbounded wildcard

From Dev

Java generic wildcard bounded in lists

From Dev

Are super and extends exclusive in generic wildcard?

From Dev

Are super and extends exclusive in generic wildcard?

From Dev

Unbounded wildcard generic array from supplier in enum

From Dev

Unbounded wildcard generic array from supplier in enum

From Dev

Java Generic Type for Wildcard extends allow to add only null

From Dev

Unbounded Wildcard Type Parameter vs Abstract Type

From Dev

Doubly-Wildcard Generic Type error

From Dev

Irregularities with the (?) wildcard generic type

From Dev

Java generic type with wildcard

From Dev

Irregularities with the (?) wildcard generic type

From Dev

Why there is no warning while casting from object to unbounded wildcard collection?

From Dev

Wildcard with extends

From Dev

Bounded wildcard in return type of static factory pattern

From Dev

Where does the Type Annotation belong in a bounded wildcard?

From Dev

List of List of Numbers with a Bounded wildcard type

From Dev

How to pass bounded wildcard type argument in Kotlin?

From Dev

Bounded wildcard in return type of static factory pattern

From Dev

Using bounded wildcard when class has bounded type parameters

From Java

What is the difference between Java vararg with direct type vs wildcard generic via extends?

From Dev

What is the difference between Java vararg with direct type vs wildcard generic via extends?

From Dev

Scala not resolving X as Y, even though X extends Y

From Dev

What is the practical usage of an array of an unbounded wildcard parameterized type?

Related Related

  1. 1

    Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

  2. 2

    Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

  3. 3

    Generic bounded wildcard of function input type

  4. 4

    Java generic upper bounded wildcard error

  5. 5

    generic wildcard is accepted but not Type T

  6. 6

    Type parameter vs unbounded wildcard

  7. 7

    Java generic wildcard bounded in lists

  8. 8

    Are super and extends exclusive in generic wildcard?

  9. 9

    Are super and extends exclusive in generic wildcard?

  10. 10

    Unbounded wildcard generic array from supplier in enum

  11. 11

    Unbounded wildcard generic array from supplier in enum

  12. 12

    Java Generic Type for Wildcard extends allow to add only null

  13. 13

    Unbounded Wildcard Type Parameter vs Abstract Type

  14. 14

    Doubly-Wildcard Generic Type error

  15. 15

    Irregularities with the (?) wildcard generic type

  16. 16

    Java generic type with wildcard

  17. 17

    Irregularities with the (?) wildcard generic type

  18. 18

    Why there is no warning while casting from object to unbounded wildcard collection?

  19. 19

    Wildcard with extends

  20. 20

    Bounded wildcard in return type of static factory pattern

  21. 21

    Where does the Type Annotation belong in a bounded wildcard?

  22. 22

    List of List of Numbers with a Bounded wildcard type

  23. 23

    How to pass bounded wildcard type argument in Kotlin?

  24. 24

    Bounded wildcard in return type of static factory pattern

  25. 25

    Using bounded wildcard when class has bounded type parameters

  26. 26

    What is the difference between Java vararg with direct type vs wildcard generic via extends?

  27. 27

    What is the difference between Java vararg with direct type vs wildcard generic via extends?

  28. 28

    Scala not resolving X as Y, even though X extends Y

  29. 29

    What is the practical usage of an array of an unbounded wildcard parameterized type?

HotTag

Archive