How do generic types fit into the type hierarchy?

Viewed 45
1 Answers

List and Map are not types; they're classes.  To form a type, you need to specify the necessary type parameters, e.g. List<Int> (or a type projection such as List<*>).

(This is different from Java, which allows raw types, mainly for backward compatibility.)

The type relationships depend upon the variance: for example, List<Int> is a subtype of List<Number>, because it's covariant in its type parameter; but MutableList<Int> is invariant and has no relationship with MutableList<Number> (except that they're both subtypes of Any).

This adds extra dimensions to the type hierarchy, making it rather more complex than the simple graph you may have seen.  But the principle's straightforward enough, so it's not usually drawn in.

More details, as ever, in the Kotlin documentation.

Related