What Is Type Erasure?
Type erasure is a technique where the concrete type arguments of generic code are removed and replaced by a shared representation, such as a common base type or a boxed value. A single compiled version then handles all type arguments through that uniform interface. It keeps code size small at the cost of runtime dispatch or boxing.
Why it matters
Erasure produces one copy of generic code regardless of how many types use it, keeping binaries and compile times smaller than monomorphization. The downside is indirection and the loss of some type information at runtime.
Related guides
What Is Monomorphization?Monomorphization compiles generic code into a separate specialized copy for each concrete type it is used wit…
What Is Trait Object Dispatch?Trait object dispatch calls a method through a runtime pointer to a table of implementations, choosing behavi…
What Is Boxing?Boxing wraps a value type in a heap-allocated object so it can be used where a reference or common interface…