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
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
  • 3,607
  • 1
  • 20
  • 30
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
  • 355
  • 2
  • 6
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
  • 530
  • 6
  • 13
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
  • 31,683
  • 83
  • 267
  • 574
2
votes
0 answers

Difference between Replication and Erasure coding techniques

I hope I am asking in the right community, If not, any suggestion will be appreciated. I am doing a survey paper where I am doing a comparison between erasure coding and replication techniques. At this stage, I am comparing them regarding specific…
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,658
  • 13
  • 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
  • 1
  • 10
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
  • 19,054
  • 9
  • 76
  • 156
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
  • 38,251
  • 58
  • 190
  • 340
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,011
  • 2
  • 26
  • 67
2
votes
1 answer

difference behaviour for generic method within generic class and for generic method within non-generic class

I investigate generic behaviour I noticed that: public class Hohol1 { public class My { public void test(Collection es) { System.out.println("Collection"); } public void test(List integerList) { …
gstackoverflow
  • 31,683
  • 83
  • 267
  • 574
2
votes
2 answers

Using scala generics and manifest to cast within a class

I have two classes, Holders (for lack of a better name at the moment) and Holder. Holder has to be interfaced through Holders, which has an array of Holder of any type. As such, it has to take Any type. I want to have the setValue do type checking…
James Lee
  • 75
  • 3
2
votes
3 answers

How would I implement a widen function on Function1 or PartialFunction

I would like to define a widen function on Function1 or PartialFunction. I want to do this because I have a use case similar to the following: class A class B extends A def foo(fun: Function[B, A]) = { bar(fun.widen[A]) } def bar(pf:…
jedesah
  • 2,835
  • 1
  • 15
  • 27