Type erasure

Kotlin is designed primarily as a language for the Java Virtual Machine (JVM), and when the JVM was first designed, generics were not included as a feature. Over time it became apparent that this was a major flaw of the language, and so in Java 1.5 (or Java SDK 5), released in 2004, generics were added as a feature to the compiler.

However, because of a desire to stay backwards compatible with previous versions of Java, the designers of Java decided that generics would be implemented using a technique called erasure. Erasure is the name given to the process by which the compiler removes type parameters during compilation.

In Java, a class defined as List<T> in the source code would be compiled simply as List, or List<Object>, if you ...

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.