Created
October 14, 2015 09:25
-
-
Save sou-hi/4d4aa5ed3941998096bf to your computer and use it in GitHub Desktop.
asInstanceOfOption ref: http://qiita.com/sou-hi/items/a87641cdfa8140075065
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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