type-erasure

Designing constructors around type erasure in Java

Yesterday, I was designing a Java class which I wanted to be initalized with Lists of various generic types: TheClass(List<String> list) { ... } TheClass(List<OtherType> list) { ... } This will not compile, as the constructors have the same erasure. I just went with factory methods differentiated by their names instead: publi...

Java class object from type variable

Is there a way to get Class object from the type variable in Java generic class? Something like that: public class Bar extends Foo<T> { public Class getParameterClass() { return T.class; // doesn't compile } } This type information is available at compile time and therefore should not be affected by type erasure, so, t...

Feature or Bug:Why does this Java code compile?

Possible Duplicate: Is this valid Java? I was surprised to discover the Java class below compiles. It has several method, with the same name, number of arguments and following type-erasure types of argument. Yet, it compiles and works as expected, on windows using various versions of the Sun JDK 1.6 compiler. So if this is a ...

Scala double definition (2 methods have the same type erasure)

Hi I wrote this in scala and it won't compile: class TestDoubleDef{ def foo(p:List[String]) = {} def foo(p:List[Int]) = {} } the compiler notify: [error] double definition: [error] method foo:(List[String])Unit and [error] method foo:(List[Int])Unit at line 120 [error] have same type after erasure: (List)Unit I know JVM has no...

How can I differentiate between def foo[A](xs: A*) and def foo[A, B](xs: (A, B)*)?

I know that type erasure makes them look equal, type-wise, at runtime, so that: class Bar { def foo[A](xs: A*) { xs.foreach(println) } def foo[A, B](xs: (A, B)*) { xs.foreach(x => println(x._1 + " - " + x._2)) } } gives the following compiler error: <console>:7: error: double definition: method foo:[A,B](xs: (A, B)*)Unit a...

[c++] Type erasure and accessors

Hi, I used type erasure pattern in C++, i.e I hide a template class with an abstract class class Base{ virtual ~Base(){} //pure virtual methods... }; template<typename T> class Derived : Base{ Derived<T>(){} ~Derived(){} //public methods... private : vector<T> datas; }; problem : if I want to retrieve or modify datas, I have...

Scala collection filter by type

Hi, im new to scala and ran into the following problem: I want to get a subcollection of an existing collection that only contains elements of a specific type. The following works: class C(val name : String) class D(name : String) extends C(name) { } val collection = Set[C](new C("C1"),new D("D1"),new C("C2"),new D("D2")) collection.c...

Cannot compile a class which implements an interface without type parameter

I have the following test code: public interface Container<I> { public void addClass(Class<?> clazz); } public class MyContainer implements Container { public void addClass(Class<?> clazz) {} } and I get the following error when trying to compile these two class: MyContainer.java:1: MyContainer is not abstract and does no...

How do I reconstruct generic type information for classes given a TypeLiteral?

I have the following problem: Given a Guice type literal TypeLiteral<T> template and a class Class c implementing or extending T, construct a type Type t which is equivalent to c with all type variables instantiated so as to be compatible with template. If c has no type variables, it's easy; c is the type in question. However, if ...

weird behavior around "same erasure" compilation error

I recently stumbled upon a piece of code that would not compile in my Eclipse due to the "same erasure" issue (looked very similar to this one). The guys who wrote the code assured me that it compiles in their local environment and their continuous integration and so I played along to emulate it. Take a look at this snippet: package c...