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
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,555
  • 2
  • 16
  • 23
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…
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
  • 621
  • 2
  • 9
  • 22
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
  • 205
  • 3
  • 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
  • 523
  • 7
  • 19
0
votes
0 answers

Java erasure conflict in classes that implement Hazelcast MessageListener with different message objects

Currently using Hazelcast 3.0 and trying to set up different topics with different message objects and I'd like to have classes that can listen to more than one topic, but I don't see how it is possible because the differing onMessage() methods have…
tmadison
  • 193
  • 1
  • 1
  • 7
0
votes
3 answers

Generic messaging

I am working on a messaging system on C++. I have; class MessageData { public: typedef std::vector> MessageList; virtual int getValue(std::shared_ptr) { throw "Not implemented!"; }; virtual float…
Ali Naci Erdem
  • 435
  • 2
  • 12
0
votes
1 answer

Swift: Inheritance in type parameters

I'm trying to cast an object of a class with a type parameter into an object of the same class with a more generic type parameter, as illustrated in the following Swift code: class ParameterClassA { } class ParameterClassB: ParameterClassA…
David
  • 437
  • 1
  • 4
  • 15
0
votes
2 answers

it is possible to cast to wrong types without exception in scala

I wrote a code the other day to filter out mixing behavior form a list. Her is an example code which should describe the problem I ran into. def myFilter[A](toFilter : Any) : Option[A] = toFilter match { case keep : A => Some(keep) case _ =>…
0
votes
0 answers

Why does Java 6 compiles this code and Java 7 does not? Both methods have same erasure

public class Summer { public static int sum(List ints) { .... } public static double sum(List doubles) { .... } } Why does Java 6 compiles this code and Java 7 does not? Java 7 reports the error…
0
votes
1 answer

Scala erasure in pattern matching

I have to extract information of a JSON response and evaluate if some file dis present or not. I'm using the following method definition: override def hasField(field: Field): Boolean = { val schema = parse(httpClient.doGet(url +…
frank_neff
  • 952
  • 8
  • 16
0
votes
2 answers

Scala Erasure Type Match and Use in Different Method

I have been searching around to achieve this, even with Manifest and Reflect API, it's still hard to achieve. With Manifest and Reflection, I can match List[Any] to a class(List[A]), I am also able to get match by type T, just as in…
0
votes
2 answers

scala getClass and type erasure?

I am a little puzzled by the getClass method when used in generic type case: def a[K](key: K) = { println(key.getClass) } Shouldn't this always been AnyRef or Any or something? Since type K info is not available in runtime? but a(3) will output…
Raymond
  • 73
  • 1
  • 7