DELAPARRA
Index
Color
NL
Category: Programming paradigms

Type Erasure

Explained by Updated

What is Type Erasure?

Type Erasure: In some languages (Java), generic type info is removed at compile time and only checked statically — it doesn't exist at runtime. Type erasure means generic type information is removed when code is compiled. Java checks that a List<String> only holds strings at compile time, but at runtime it is just a List of objects.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Type Erasure

How is Type Erasure used in practice?

This kept Java's generics compatible with older code, and avoids generating separate versions of each generic class. The cost is that you cannot ask at runtime what type a generic list was declared with, or create an array of a generic type directly.

What should you keep in mind with Type Erasure?

Other languages make other choices. C# keeps generic types at runtime, and Rust and C++ generate specialised code per type. PHP has no runtime generics at all; PHPStan and Psalm check generic annotations statically, which is effectively type erasure by design. The practical effect is that runtime checks still need to validate data coming from outside the program.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with T

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.