Search code examples
javatype-inferencejls

Java JLS, the supertype set of a type


This section of the JLS describes what ST(U) is, where U is a type:

Let ST(Ui) be the set of supertypes of Ui.

And a few lines under that, in the same section, there is an example:

For example, given List<String> and List<Object>, simply intersecting the sets ST(List<String>) = { List<String>, Collection<String>, Object } and ST(List<Object>) = { List<Object>, Collection<Object>, Object } would yield a set { Object }, and we would have lost track of the fact that the upper bound can safely be assumed to be a List.

Shouldn't ST(List<String>) also include Iterable<String>?


Solution

  • You're right, ST(List<String>) should contain Iterable<String>. Moreover, ST(List<String>) should contain List and all supertypes of List, since by JLS 8 Section 4.10.2,

    Given a generic type declaration C<F1,...,Fn> (n > 0), the direct supertypes of the parameterized type C<T1,...,Tn>, where Ti (1 ≤ i ≤ n) is a type, are all of the following:

    • ...
    • The raw type C.

    so this example doesn't actually demonstrate what it's trying to demonstrate, because List actually is in the intersection of ST(List<String>) and ST(List<Object>), before ESTs come into the picture.

    I think the example may have been written for an earlier version of the supertyping rules and then not updated when the rules changed. The Java 7 JLS defines the supertype relationship differently, but even then, I think Iterable<String> would have been a supertype of List<String>.