class S
class A extends S
class B extends S
class ClassWithTypeParameter[+T]
val a: ClassWithTypeParameter[S] = new ClassWithTypeParameter[A]
How can one determine the type of the subclass used for the type parameter of value a ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cannot because of type erasure. However, I would argue that your attempt to do this is formed from a mis-understanding.
In your program, there is one reference,
aand the type of this reference isClassWithTypeParameter[S]. That. Is. All. The compiler can know what can be done with this reference. The types are there purely for the compiler. The fact that, at runtime,awas assigned to a value which was aClassWithTypeParameter[A]is irrelevant.One possible way of doing this to some approximation (limited by erasure) is to use manifests (called something else in 2.10):
Then you can call
erasurewhich will get you ajava.lang.Classback. As I said, this is limited. A class is not the same thing as a type and there is no way to distinguish, for example,ClassWithTypeParameter[List[Int]]fromClassWithTypeParameter[List[Double]]