Created
May 6, 2010 01:06
-
-
Save fedesilva/391667 to your computer and use it in GitHub Desktop.
Autoboxing stuff in Option[T]
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
f@Hedwig:~$ scala | |
Welcome to Scala version 2.8.0.r21323-b20100403020201 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
//Some fun | |
scala> def fun(integer:Option[Int]) = println(integer.getOrElse("None")) | |
fun: (integer: Option[Int])Unit | |
// having fun | |
scala> fun(None) | |
None | |
// not that much fun. verbose | |
scala> fun(Some(1)) | |
1 | |
// no fun! | |
scala> fun(1) | |
<console>:7: error: type mismatch; | |
found : Int(1) | |
required: Option[Int] | |
fun(1) | |
^ | |
//Do it for me please, so it's more fun | |
scala> implicit def boxInSome[T](any:T):Option[T] = any match { case None => None case _ => Some(any) } | |
boxInSome: [T](any: T)Option[T] | |
//fun! | |
scala> fun(1) | |
1 | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment