Questions tagged [erasure]

When implementing generics in a programming language often the binary does not contain any of the type information from the generics. This is referred to as "erasure"

Java is famous (and often criticized) for using erasure. This choice was made to keep the language compatible with older versions which was always an important design goal for java first by Sun and later by Oracle.

Other similar languages like C# keep the type information from generics which allows to use this information at runtime.

Scala which compiles to Java byte code also has to use erasure, but uses other language features like Manifests to solve the problem of missing type information at runtime at least to some extend.

The tag should be used for question regarding the way erasure works and problems arising from erasure.

76 questions
28
votes
2 answers

How does Gson TypeToken work?

I understand that in Java contrary to, for example, C# generics are compile-time feature and is removed via type erasure. So, how does Gson's TypeToken really work? How does it get the generic type of an object?
Heisenberg
  • 3,001
  • 3
  • 24
  • 50
22
votes
1 answer

Pattern matching on generic type in Scala

I have scala function that looks like this: Now, depending upon the type of T (In my case, it can be Double, Boolean and LocalDate), I need to apply functions on ob. Something like this (I know the code will make no sense but I am trying to convey…
Core_Dumped
  • 4,077
  • 9
  • 36
  • 67
14
votes
3 answers

Implementing Comparable, compareTo name clash: "have the same erasure, yet neither overrides the other"

I'd like to have a compareTo method that takes a Real (a class for working with arbitrarily large and precise real numbers [well, as long as it's less than 2^31 in length at the moment]) and a compareTo method that takes an Object, but Java isn't…
Jxek
  • 373
  • 1
  • 3
  • 13
11
votes
1 answer

Why does using raw type variables affect signatures without reference to type-parameters?

Looking into another question I bumped into this intriguing behavior of the 1.8.0_112 Sun-Oracle compiler (I have not tested with others): import java.util.List; interface Alpha { List intList(); } interface Beta { List
Valentin Ruano
  • 2,574
  • 15
  • 28
11
votes
1 answer

Java generics type erasure

I'd like to ask about java type erasure rules. If we have classes: public class Shape{} public class Circle extends Shape{} public class Base{ T x; public void setX(T t){} } public class MainClass(){ public static…
Iza Marek
  • 231
  • 1
  • 5
10
votes
3 answers

How is the Java erasure affecting the generic arrays?

I'm studying generics in this period and today I've found this mystery for me. Let's consider the following dummy class: public class Main{ public static void main(String[] args) { Container c = new Container();…
acejazz
  • 617
  • 6
  • 18
9
votes
3 answers

Java & Kotlin casting with generics. Losing typesafety

When coding in Kotlin/Java, I stumbled onto something rather odd while using casting and generics. It seems to be possible to have the type system believe a list is of the type List, while it is actually a List. Can anyone explain to me…
Tobiah Lissens
  • 135
  • 2
  • 9
8
votes
3 answers

The difference between instanceof List and instanceof List

I know that we cannot invoke instanceof List because List is not a reifiable type. Both instanceof List and instanceof List work; however the eclipse IDE suggests use instanceof List. I wonder why it suggests unbound wildcard instanceof…
Linh
  • 325
  • 1
  • 3
  • 10
8
votes
1 answer

Two methods with same erasure aren't necessary override-equivalent (or they signatures aren't subsignatures between them)?

Im reading the incredible book “a programmer's guide to java scjp certification” for jdk6 and theres a section about generic overriding. On it is described subsignature and override-equivalent and describes some examples of override-equivalent that…
user1546652
  • 557
  • 1
  • 5
  • 15
8
votes
2 answers

Manifest[T].erasure is deprecated in 2.10, what should I use now?

I have the following code: object Log { def get[T](implicit manifest : Manifest[T] ) = { LoggerFactory.getLogger( manifest.erasure.getName ) } def getByName( name : String ) = { LoggerFactory.getLogger(name) } } The idea is to…
Maurício Linhares
  • 37,947
  • 14
  • 116
  • 153
7
votes
5 answers

ArrayList automatically change its type to ArrayList

public void run(){ setFont("Courier-24"); //Define list as ArrayList ArrayList list = new ArrayList(); readList(list); } private void readList(ArrayList list){ list.add("Hello"); list.add(2); …
kahofanfan
  • 297
  • 1
  • 10
7
votes
3 answers

Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure

Would the Haskell equivalent of the code below produce correct answers? Can this Scala code be fixed to produce correct answers ? If yes, how ? object TypeErasurePatternMatchQuestion extends App { val li=List(1,2,3) val ls=List("1","2","3") …
jhegedus
  • 18,516
  • 11
  • 84
  • 147
6
votes
2 answers

Why does erasure still allow overriding/implementation?

At first glance I thought the following makes sense: interface Test { T getValue(T n); } class Impl implements Test{ public Integer getValue(Integer n){ return n; } } And it compiles properly so everything seems…
kng
  • 587
  • 2
  • 9
5
votes
4 answers

Duplicate Method while Method-Overloading

Following code gives compilation error with error "Duplicate Method" static int test(int i){ return 1; } static String test(int i){ return "abc"; } This is expected as both the overloaded method have same signature and differ only in…
Abhinav
  • 1,665
  • 2
  • 19
  • 24
4
votes
1 answer

Type rules of arrays and generics in Java

In the last paragraph of Item 25 in the Effective Java (2nd), it's said: Arrays and generics have very different type rules. Arrays are covariant and reified; generics are invariant and erased. Could someone give better definitions of the bolded…
flowjow
  • 161
  • 2
  • 13
1
2 3 4 5 6