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
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
  • 36,709
  • 117
  • 359
  • 710
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,983
  • 2
  • 17
  • 29
1
vote
1 answer

Override method in extended Class with same erasure

So I have some classes like BooleanLogger, IntLogger etc., which implement my own Logger interface. This is the Logger interface: public interface Logger { String serialise(); Object deserialise(String value); void setValue(Object…
J P
  • 541
  • 1
  • 10
  • 20
1
vote
0 answers

Erasure vs implicit cast for multiple bounded types in a Generic Class/Method

According to JLS§4.4: The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position. Consider the…
theutonium.18
  • 483
  • 2
  • 7
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
  • 2,125
  • 2
  • 21
  • 38
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
  • 4,440
  • 3
  • 32
  • 41
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
  • 63
  • 7
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
  • 11
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
  • 297
  • 2
  • 11
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
  • 297
  • 2
  • 11
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
  • 175
  • 1
  • 16
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
  • 18,150
  • 10
  • 103
  • 108
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…
Ky -
  • 30,724
  • 51
  • 192
  • 308