Generic Type Erasure and Constraints
Understand the concept of type erasure, constraints, and star projection in Kotlin's generics.
Type erasure
Generic types were added to Java for developers’ convenience, but they were never built into the JVM platform. All type arguments are lost when we compile Kotlin to JVM bytecode. Under the hood, this means that List<String>
becomes List
, and emptyList<Double>
becomes emptyList
. The process of losing type arguments is known as type erasure. Due to this process, type parameters have some limitations compared to regular types.
We cannot use them for
is
checks.We cannot reference them.
We cannot use them as reified type arguments.
Get hands-on with 1300+ tech skills courses.