I have instance and type and want to get Some
in case casting is possible and None
if not.
Currently I use my own implementation:
def tryCast[T](o: Any)(implicit manifest: Manifest[T]): Option[T] = {
val clazz = manifest.runtimeClass.asInstanceOf[Class[T]]
if (clazz.isAssignableFrom(o.getClass)) {
Some(o.asInstanceOf[T])
} else {
None
}
}
Is there any method in standard library that I can use instead?