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
1
vote
1 answer

Getting weird output when trying to print StringBuilder type: "com.jeanlucthumm.poem.Word@7852e922"

My program aims to create a random poem by repeatedly printing a randomly generated line. I have a class called Line which has a field line that it operates on: private StringBuilder line = new StringBuilder(); The constructor looks like…
jeanluc
  • 1,439
  • 1
  • 10
  • 24
1
vote
1 answer

Scala ambiguous reference to overloaded definition with two implicit parameters

lazy val productService = BeanLookup[ProductDataService] object BeanLookup { def apply[T](implicit manifest: Manifest[T], context: ActorContext) = { val beanLookup = context.actorFor("/user/spring/beanLookup") …
Deil
  • 422
  • 3
  • 11
0
votes
0 answers

Why do Java Anonymous Classes retain generic information at runtime?

While I was looking for a way to retain information about generic types at runtime in the context of JSON (de)serialization with Jackson, I found out that it's indeed possible to do so using TypeReference. I then wanted to understand how and why…
gscaparrotti
  • 431
  • 2
  • 9
0
votes
0 answers

Does Quantitative Type Theory make the Prop universe obsolete?

Coq (and other type theories such as Setoid Type Theory) have a Prop universe for propositions. As far as I understand this universe is needed to be sure that the propositions can be erased. In Quantitative Type Theory one can explicitly say which…
Labbekak
  • 527
  • 3
  • 12
0
votes
1 answer

How do I avoid this erasure error in Java?

I have a homemade storage object that is similar to Set interface methods. I want to make it compatible with Set so it can be compatible with Collections. The problem is that it is a generic class and that generic class uses the type variable as…
0
votes
2 answers

Conflict between return type and method parameter when return type is 'T' and method parameter consist of wild card

I'm trying to run a code. i and i get two compilation errors: 1.Reference to System.out.println is ambiguous (conflict between method that gets char[] and a method that gets a String) 2.Cap#1 can't converted to T return st.pop() import…
Eitanos30
  • 1,089
  • 7
  • 9
0
votes
1 answer

Erasure type and Bridging Method clarification

The code following is taken from Oracle documentation of generics - class Node { public T data; public Node(T data) { this.data = data; } public void setData(T data) { System.out.println("Node.setData"); this.data…
lynxx
  • 514
  • 3
  • 11
0
votes
0 answers

Java error: name clash: method in Class overrides a method whose erasure is the same as another method, yet neither overrides the other

I have code in my project like this below: public class CommandValidator extends AbstractValidator { @Override public boolean validate(CommandTransaction object) { return false; } } public abstract class…
0
votes
0 answers

Why can't we overload methods based on generic type of list

I know that because of type erasure, all generic parameters are erased during compilation so if we have the following methods public void addToList(List list){} public void addToList(List list){} They will both end up looking…
GamefanA
  • 1,346
  • 2
  • 13
  • 22
0
votes
1 answer

Compiler asking for optional methods from Collections to be overridden

I'm creating a class-wide project with my school mates, I'm supposed to create pull requests for some functions, but I'm having some problems just creating the class and overriding the methods in a way that it would just compile (I don't need to…
Luke Perez
  • 332
  • 1
  • 11
0
votes
1 answer

Java Generics: Example if types are not erased at compilation

In Java, generic types are erased at compilation and instead Object is substituted for all generic parameters and an implicit cast is done. The reason for doing this is to maintain backward compatibility as explained here. Can anyone give a code…
qrius
  • 601
  • 1
  • 7
  • 20
0
votes
1 answer

Same erasure but not the same type.

How can it be the case that ArrayList> has the same erasure of ArrayList but cannot be cast to it? My code is currently switching between two eclipse errors. GenericExclusiveSelectionPanel transport = new…
0
votes
1 answer

Is type erasure concept exist in C-language?

I wonder if type erasing techniques are used anywhere in C language. What happens in C, when type casting is done? Does it use concepts similar to type erasure and down casting? What are the main difference between type erasure and type casting?
NaveeNeo
  • 195
  • 2
  • 13
0
votes
1 answer

Name clash because of same erasure

I have an interface like the following public interface IDrawerItem extends IItem, IExpandable, ISubItem { void bindView(VH holder, List payloads); } Im…
user3600801
0
votes
2 answers

Java 1.6 -> 1.8, same erasure compile error

I'm porting my Java app from 1.6 to 1.8 and the compiler is unhappy with the methods getAbstractTransactionCriteria() in the following code: public abstract class AbstractTransaction ... public class TemplateTransaction extends AbstractTransaction…
skiaddict1
  • 453
  • 3
  • 17