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

How to distinguish parameteric types?

I am confused about subtypes in Scala. My major question is how to distinguish C[T1] from C[T2]. There are two scenarios: C[T1] "equals" C[T2] because they are all subtypes of C. C[T1] does not "equal" C[T2] because C[T1] and C[T2] are different…
calvin
  • 859
  • 9
  • 22
1
vote
2 answers

Gson desearilize list and class, how does erasure work here?

I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static String toJson(T object)…
TheChetan
  • 3,887
  • 3
  • 27
  • 36
1
vote
1 answer

Type Erasure in ArrayList> generic call

Can someone explain why the following code does not compile: ArrayList> arrayList = new ArrayList>(); Why is the above code invalid but this one is running fine: ArrayList items = new ArrayList>();
Cybazaar
  • 13
  • 2
1
vote
1 answer

How to get overloaded method to recognize specific subclass of generic type?

I have a method which is often called with an argument of generic type, and I want it to behave differently depending on the specific subclass of the argument. For example, the code below: public class Foo { public static void fooMethod(Foo f)…
eRedekopp
  • 41
  • 6
1
vote
2 answers

How is Implicit finding the correct method to be invoked with generics when types are erased?

In my code below, I have a function test that takes in a object of type magnet and I have two implicit methods converting List[Int] to magnet and the other converting List[String] to magnet. If JVM is supposed to loose the types in generics due to…
Aandal
  • 51
  • 2
  • 10
1
vote
2 answers

How to Cast Generic Type from Erasure

I have a parent class, Parent, with two child classes, A and B. I have an interface, Function, that allows the programmer to write a specific function, Type2 of(Type1 t), that takes a Type1 to a Type2. The…
user2303321
  • 187
  • 1
  • 10
1
vote
1 answer

How to use Inherited Methods using Type Erasure?

I'm trying to use type erasure in order to access the inherited methods of the general type. I have a parent class, Space, and child classes, Scalar, Vector, Mapping, etc. I want an interface, Function, for which I can specify the domain and range…
user2303321
  • 187
  • 1
  • 10
1
vote
0 answers

SAP Hybris Commerce 6.2 Cuppy Project Setup Issues

Name clash: The method convertAll(Collection) of type GenericCollectionConverter has the same erasure as convertAll(Collection) of type Converter but does not override it Below files giving the error above while doing Ant…
sagar
  • 165
  • 1
  • 13
1
vote
1 answer

Scala work-around for methods with same type after erasure

I figured out how to use a TypeTag to add a empty parameter list to an existing method and bypass the erasure error. I'd like to understand how my hack works and if there is a better way to achieve the desired outcome. I have the following…
Powers
  • 12,561
  • 7
  • 60
  • 82
1
vote
2 answers

Setter for field is removed by type projection

I have the following SSCCE: class Foo(val bars: Map>) { fun qux(baz: Baz) { val bar2 = bars[2]!! bar2.bazes += baz } interface Bar { var bazes: MutableList } } This seems fine to…
Ben Leggiero
  • 25,904
  • 38
  • 161
  • 267
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
  • 1,856
  • 11
  • 25
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
  • 8,783
  • 21
  • 69
  • 90
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
  • 617
  • 6
  • 18
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
  • 155,475
  • 189
  • 518
  • 818
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,343
  • 2
  • 22
  • 33