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.

82 questions
3
votes
1 answer

Generic Array initialisation

I have read through different articles which talks about why we cannot create generic array in java, but still I don't quite understand why. For example, it this post, it assumed if generic array initialisation is possible, there will be casting…
HKIT
  • 628
  • 1
  • 8
  • 19
3
votes
2 answers

Checking scala types at runtime and type erasure

So say we have a few classes like this: abstract class Throw { def winsOver(t2: Throw): Boolean } class Rock extends Throw { override def winsOver(t2: Throw): Boolean = t2 match { case _: Scissors => true case _ …
3
votes
2 answers

abstract type T is unchecked since it is eliminated by erasure

I am writing a Scala class which extends a Java class. I must extend this abstract Java class because it contains some converters, and I need to write a converter of my own that I can plug into their framework. So the signature of the method below…
radumanolescu
  • 4,059
  • 2
  • 31
  • 44
3
votes
2 answers

Java generics - erasure concept

I have some code as follows: public class java_generic { public static void main(String[] args) { T t = new X(); t.m(new Object()); t.m(new String()); } static class T { void m (E…
Kone Man
  • 374
  • 1
  • 2
  • 8
3
votes
2 answers

Scala - Abstract types and Implicit Parameter Resolution

I'm using Scala 2.10.4. Please bare with the analogy - the actual code is deeply embedded in a complicated program, so rather than explain that, I’ll abstract the problem in a time honoured way to talk about Animals ;-) In scala I have 2 traits -…
Phil
  • 592
  • 6
  • 15
3
votes
3 answers

Differences just List and List and List
According erasure concept I thought that List and List are indentically but I noticed that List strList = new ArrayList(); List objList = strList; //error List objList = strList; //valid…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
2
votes
2 answers

Java generic List name clash, has same erasure

I am writing a method with a generic List as an argument. I want to limit T to Integer, Float and Double with this: private Method(List list) { this.list = list; } public static Method create(List list) { …
2
votes
0 answers

Overloaded methods in the class must have different Erasures - why only leftmost bound is considered to be the Erasure type?

Consider the following two method definitions: static T whatToDo(T ele){return null;} static R whatToDo(R ele){return null;} Both will have the following erasures respectively: static Do…
theutonium.18
  • 483
  • 2
  • 7
2
votes
0 answers

Scala: existential type and type erasure

I'm not very familiar with scala so I hope that my question makes sense: I have a complex parametrised class, but lets resume it to: class Foo[T] { def foo(x: T) = {/*do something*/} } Now In another part of the program, I would like to build…
AntoinePrv
  • 21
  • 3
2
votes
3 answers

Java type inference with erasure

having some problem with erasure & type inference. I have the following class hierarchy, which doesn't look very complicated: public class Foo { } public class Bar { private final Class clazz; public Bar(Class clazz) { …
Stas
  • 1,707
  • 15
  • 25
2
votes
2 answers

an example of type erasure and my understanding

private void readList(ArrayList list){ list.add("Hello"); list.add(2); } public void run(){ setFont("Courier-24"); ArrayList list = new ArrayList(); readList(list); println("list = "+list); …
kahofanfan
  • 297
  • 2
  • 11
2
votes
2 answers

Java Type Erasure: Rules of cast insertion?

The Java tutorial on type erasure doesn't seem to detail the specific rules of cast insertion by the compiler. Can someone please explain the specific rules that cause the transformation detailed by the tutorial (reproduced below): public class…
Kvass
  • 8,294
  • 12
  • 65
  • 108
2
votes
1 answer

Java erased -> compile time type alignment - or Java Library/Framework code transitioning to application logic

I am Working on a configuration scenario for a complex distributed OSGi system. I need to make the following transition from library code to application code on Java 7 (this code below is incorrect): void someFrameworkMethod(...) { .... //…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
2
votes
2 answers

Understanding Erasure with Generic Case Class

For the following case class: scala> case class Foo[T](name: String) {} defined class Foo scala> val foo = Foo[Int]("foo") foo: Foo[Int] = Foo(foo) Why will Scala let me, as I think it's doing, match on Foo[Int]? Isn't the Int erased? scala> foo…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
2
votes
1 answer

Ambiguous constructor due to type erasure

I have a fragment of legacy source code which looks like this: import javax.management.MBeanParameterInfo; import javax.management.openmbean.OpenMBeanParameterInfoSupport; import javax.management.openmbean.OpenType; class C { void f() { …
Bass
  • 4,977
  • 2
  • 36
  • 82