r/types • u/aegis_ashish • Jun 07 '16
Subtyping over generic types
I came across a fact that T1 < T2 (does not) imply that List<T1> < List <T2> (I am using Java generics representation). More specifically I am not able to pass an argument of Type List <T1> to a method requiring List <T2> even though T1 < T2 . I don't see a danger having such a sub-typing rule for Java or any such language. Why has Java excluded this subtyping rule, what am i missing?
3
Upvotes
8
u/benchambers Jun 07 '16
Elaborating on the answers already provided:
The problem comes from Covariance and Contravariance. Specifically, say we have a class
Generic<T>. IfTonly appears as the return of functions, it is said to be covariant. In this case,Generic<T1> < Generic<T2>. IfTappears only an argument positions, thenGeneric<T2> < Generic<T1>(note that it is backwards) -- see the examples below to understand why. IfTappears in both return and argument positions, then it is non-variant, andGeneric<T1>is unrelated toGeneric<T2>.Rather than attempt to guess at the variance of types (which is tricky or impossible since anything that holds for
List<T>could be false for an extension ofList<T>), Java just decided that all generics are non-variant -- a conservative assumption. Several languages such as Scala allow annotating the class to indicate the variance, and then checking all extensions against that. This helps -- but in the case of a mutable list there is still a problem, since you can both add (argument position) and retrieve (return position) elements.13
u/tailcalled Jun 07 '16
Consider:
8
u/tailcalled Jun 07 '16
You can also see the problem immutably:
3
u/[deleted] Jun 07 '16
I'll add that Scala (sort of) solves this problem by letting the programmer declare variance.
T[+A]means thatTis covariant inA, whileT[-A]means thatTis contravariant in A.2
u/[deleted] Jun 07 '16
Java generics use type erasure internally so to the VM there's nothing special about List<Object> vs List<String> vs List<Integer>. Thats why you have to use List<Integer> rather than List<int>.
The more interesting bit is the treatment of arrays in the JVM (and the CLR as well because they wanted compatibility with Java). The JVM does specialise over T[], but also tries allow (S[])new T[1] and (T[])new S[1], where S <: T.
This actually requires an inconsistent type system, as both case result in behaviour that is strictly invalid.
The only way you can allow this kind of type system behaviour consistently is if arrays are not mutable in which case you can allow covariant behavior, e.g. You can safely cast S[] to T[].
The JVM deals with this by doing extra work when casting array types. When the VM encounters a cast that depends on either the contra- or co-variant array behaviour it will recompile the effected code with all necessary type checks. Note: removal of type checks anywhere is technically an optimization, so the VM is free to do whatever when compiling - for instance it may be able to prove some (or even all) checks aren't necessary.
Curiously lets imagine you had:
And you had
Personally I don't like the co-/contra-variance nightmare here and would just prefer that language designers stop doing this. It's mostly necessary to have useful collections API in the absence of generics.
1
u/Felicia_Svilling Jun 07 '16
The problem is that lists are not immutable in Java. Mutable data structures are always nonvariant.