Questions tagged [type-erasure]

In Java, type erasure is the process where the compiler removes all information related to type parameters and type arguments within a class or method when a generic type is instantiated. In C++ type erasure refers to a technique for hiding some or all of the type information regarding a class.

In Java, generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called type erasure.

As an example, Box<String> is translated to type Box, which is called the raw type.

Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

See Oracle's Java Tutorial on Type Erasure for more information, as well as the Wikipedia entry on Problems with Type Erasure.

In C++ type erasure is a technique to hide some or all of the type information regarding a class (e.g. boost::any).

730 questions
381
votes
11 answers

How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?

It's a sad fact of life on Scala that if you instantiate a List[Int], you can verify that your instance is a List, and you can verify that any individual element of it is an Int, but not that it is a List[Int], as can be easily verified: scala>…
Daniel C. Sobral
  • 284,820
  • 82
  • 479
  • 670
286
votes
17 answers

What's the reason I can't create generic array types in Java?

What's the reason why Java doesn't allow us to do private T[] elements = new T[initialCapacity]; I could understand .NET didn't allow us to do that, as in .NET you have value types that at run-time can have different sizes, but in Java all kinds of…
devoured elysium
  • 90,453
  • 117
  • 313
  • 521
249
votes
7 answers

Java generics type erasure: when and what happens?

I read about Java's type erasure on Oracle's website. When does type erasure occur? At compile time or runtime? When the class is loaded? When the class is instantiated? A lot of sites (including the official tutorial mentioned above) say type…
afryingpan
136
votes
6 answers

Type erasure techniques

(With type erasure, I mean hiding some or all of the type information regarding a class, somewhat like Boost.Any.) I want to get a hold of type erasure techniques, while also sharing those, which I know of. My hope is kinda to find some crazy…
Xeo
  • 123,374
  • 44
  • 277
  • 381
103
votes
11 answers

What are the benefits of Java's types erasure?

I read a tweet today that said: It's funny when Java users complain about type erasure, which is the only thing Java got right, while ignoring all the things it got wrong. Thus my question is: Are there benefits from Java's type erasure? What…
vertti
  • 6,847
  • 3
  • 43
  • 77
69
votes
11 answers

Scala double definition (2 methods have the same type erasure)

I wrote this in scala and it won't compile: class TestDoubleDef{ def foo(p:List[String]) = {} def foo(p:List[Int]) = {} } the compiler notify: [error] double definition: [error] method foo:(List[String])Unit and [error] method…
Jérôme
  • 941
  • 1
  • 9
  • 12
65
votes
2 answers

Why does unique_ptr have the deleter as a type parameter while shared_ptr doesn't?

The std::unique_ptr template has two parameters: the type of the pointee, and the type of the deleter. This second parameter has a default value, so you usually just write something like std::unique_ptr. The std::shared_ptr template has only…
R. Martinho Fernandes
  • 209,766
  • 68
  • 412
  • 492
62
votes
2 answers

Class is a raw type. References to generic type Class should be parameterized

I have the following class (from a simple Spring tutorial) public class CarValidator implements Validator { public boolean supports(Class aClass) { return Car.class.equals(aClass); } public void validate(Object obj, Errors…
user167768
55
votes
2 answers

Erasure elimination in scala : non-variable type argument is unchecked since it is eliminated by erasure

I've got a sequence Seq[Any] that has a variety of objects in it (like String, Integer, List[String], etc). I'm trying to sift through the list and break it up into separate lists partitioned based on the class type. The following is a pattern I'm…
BSJ
  • 965
  • 2
  • 8
  • 14
37
votes
4 answers

Why not remove type erasure from the next JVM?

Java introduced type erasure with generics in Java 5 so they would work on old versions of Java. It was a tradeoff for compatibility. We've since lost that compatibility[1] [2] [3]--bytecode can be run on later versions of the JVM but not earlier…
Prime
  • 3,605
  • 9
  • 43
  • 63
36
votes
2 answers

Type erasure, overriding and generics

Can someone explain to me why @Override public void fooMethod(Class c) doesn't override public void fooMethod(Class c) and gives me the following errors instead: - Name clash: The method fooMethod(Class) of type SubClass has the same…
Henrik Paul
  • 63,711
  • 30
  • 82
  • 93
36
votes
2 answers

Unchecked assignment warning

I am using Android Studio 1.1.0. This causes no warning: public static class A { public Map getMap() { return null; } } public static class B { public void processA(A a) { Map map =…
Konrad Morawski
  • 7,689
  • 6
  • 47
  • 80
35
votes
7 answers

Why following types are reifiable& non-reifiable in java?

In computing, reification has come to mean an explicit representation of a type—that is, run-time type information. oracle tutorials says , A reifiable type is a type whose type information is fully available at runtime. This includes primitives,…
Prateek
  • 11,146
  • 11
  • 54
  • 77
34
votes
3 answers

Serializing Map with Jackson

I want to serialize a Map with Jackson. The Date should be serialized as a timestamp, like all my other dates. The following code renders the keys in the form "Tue Mar 11 00:00:00 CET 1952" (which is Date.toString()) instead of the…
Florian Gutmann
  • 2,366
  • 2
  • 16
  • 24
1
2 3
48 49