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

Why can't overriding methods specify a type parameter if the overriden method doesn't?

The following code does not compile, however, if I change f(object) to f(String) or f(Integer) it compiles. I've read the other posts about the subject, but I still don't get it, how come the compiler doesn't know which method to use (in case of a…
DsCpp
  • 2,259
  • 3
  • 18
  • 46
1
vote
3 answers

Ensure different types of generics

I am trying to create a generic converter interface, which will convert T objects to U objects by using the same method name, i.e. convert: public interface GenericConverter { T convert(U fromObject); U convert(T fromObject); } Of course,…
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
1
vote
3 answers

How is the generic java erasure affecting the usage of newInstance()?

Based on the java documentation: During the type erasure process, the Java compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded, or Object if the type parameter is unbounded. Now, the…
acejazz
  • 789
  • 1
  • 7
  • 27
1
vote
3 answers

Removing "eliminated by erasure" warning in Scala

I have a simple Scala function that generates a Json file from a Map[String, Any]. def mapToString(map:Map[String, Any]) : String = { def interpret(value:Any) = { value match { case value if (value.isInstanceOf[String]) => "\""…
prosseek
  • 182,215
  • 215
  • 566
  • 871
1
vote
3 answers

Scala erasure class type parameter

I have the following setup: class Test[A](function: A => String) { def process(data: Any) { //has to be Any since it is user IO if (data of Type A) function(data) } } I cant seem to get the the typecheck to work. I…
eclipse
  • 2,831
  • 3
  • 26
  • 34
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,608
  • 1
  • 14
  • 28
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
  • 492
  • 4
  • 14
0
votes
1 answer

Scala type eraser

case class Thing[T](value: T) def processThing(thing: Thing[_]) = { thing match { case Thing(value: Int) => "Thing of int" case Thing(value: String) => "Thing of string" case _ => "Thing of something else" …
Bostonian
  • 615
  • 7
  • 16
0
votes
1 answer

Java bean mapper expected capture but is provided object

Please note: even though I mention Dozer in this question, I do believe its really just a pure Java generics question at heart. There may be a Dozer-specific solution out there, but I think anyone with strong working knowledge of Java (11)…
hotmeatballsoup
  • 385
  • 6
  • 58
  • 136
0
votes
0 answers

Java type erasure: How is type safety ensured?

As per type erasure mechanism in Java generics, a method: boolean add(E e); ..gets compiled to boolean add(Object e); But then how is type safety ensured? As add now takes Object, I can pass String, Integer, Employee etc as all are sub-types of…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
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
  • 663
  • 5
  • 21
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…
user12638373
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,331
  • 11
  • 19
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
  • 544
  • 3
  • 18
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…