Created
January 3, 2012 15:10
-
-
Save daithiocrualaoich/1555284 to your computer and use it in GitHub Desktop.
.option on nullable types
This file contains 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
implicit def nullable2Option[N >: Null](n: N) = new { | |
lazy val option: Option[N] = Option(n) | |
} | |
val s1: String = "1234" | |
s1.option // Option[String] = Some(1234) | |
val s2: String = null | |
s2.option // Option[String] = None | |
null.option // ERROR: Alas... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment