How to write "asInstanceOfOption" in Scala

Viewed 4082

Is it possible to write an "asInstanceOfOption" method that would do what is intended by the following (bogus) code?

def asInstanceOfOption[T](o: Any): Option[T] =
   if (o.isInstanceOf[T]) Some(o.asInstanceOf[T]) else None 
3 Answers
Related