No, you have to do the cast yourself — and it should be so. This cast is potentially unsafe, depending on what you want to do with the returned Class
instance. Imagine I want to roll in my own version of a cast:
def cast[T](obj: Any)(implicit m: Manifest[T]) =
m.erasure.asInstanceOf[Class[T]].cast(obj)
This is dangerous — as indicated by the unchecked asInstanceOf
. Why? Because this code runs fine with such nonsense, for instance:
val listInt = List(1, 2, 3)
val listString = cast[List[String]](listInt)
There, a List[Int]
typed as a List[String]
. And this compiles and runs fine, but you'll probably get a ClassCastException
later in your code at an unexpected line. That's why you cannot directly get a Class[T]
from a Manifest[T]
— because it is unsafe.