August 2017
Intermediate to advanced
440 pages
10h
English
Because of type erasure, incomplete type information is available at runtime. For example, type parameters of generic types are not available:
val list = listOf(1,2,3) println(list.javaClass) // Prints: class java.util.Arrays$ArrayList
This leads to a few problems. We can't perform any checks to verify what types of element List contains:
/* Compile time error: cannot check instance of erased type: List<String> */ if(collection is List<Int>) { //... }
The problem occurs because a check is performed at runtime where information about type parameters is not available. Kotlin, however, as opposed to Java, does not allow us to declare a raw type (a generic type that is not parametrized with a type argument):
SimpleList<> ...
Read now
Unlock full access