Skip to content

Instantly share code, notes, and snippets.

@sou-hi
Created October 14, 2015 09:25
Show Gist options
  • Select an option

  • Save sou-hi/4d4aa5ed3941998096bf to your computer and use it in GitHub Desktop.

Select an option

Save sou-hi/4d4aa5ed3941998096bf to your computer and use it in GitHub Desktop.
import scala.reflect._
def asInstanceOfOption[T: ClassTag](o: Any): Option[T] =
Some(o) collect { case m: T => m}
assert(asInstanceOfOption[Int](1) == Some(1))
assert(asInstanceOfOption[Int]("1") == None)
assert(asInstanceOfOption[Int](None) == None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment