I have a Java class that takes a generic type parameter and a class object related to that type:
public class Foo<T> {
public Foo(Class<? extends T> type) {
...
}
}
At first I thought I could make a clever wrapper for this in Scala:
class Bar[T](implicit m: Manifest[T]) {
...
new Foo[T](m.erasure)
...
}
But this results in a compiler error similiar to this:
error: type mismatch;
found : java.lang.Class[_$1(in value <local Bar>)] where type _$1(in value <local Bar>)
required: java.lang.Class[_ <: T]
class Bar[T](implicit m: Manifest[T]) { new Foo[T](m.erasure) }
I think I understand why this happens, but is there any way I could get the correct type of class and make this code compile?
How about
m.erasure.asInstanceOf[Class[T]]?By the way, see this question for an explanation of why
erasurereturnsClass[_]and notClass[T].