Created
September 15, 2017 03:38
-
-
Save koeninger/a388f64b8e5c20e05358f593146dfa7c to your computer and use it in GitHub Desktop.
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
scala> def useInt(i: Int) = println(s"used $i") | |
useInt: (i: Int)Unit | |
scala> val i: java.lang.Integer = null | |
i: Integer = null | |
// implicit conversion from Integer to Int fails | |
scala> useInt(i) | |
java.lang.NullPointerException | |
at scala.Predef$.Integer2int(Predef.scala:392) | |
scala> def useOptionInt(o: Option[Int]) = println(s"used $o") | |
useOptionInt: (o: Option[Int])Unit | |
scala> val optInteger: Option[java.lang.Integer] = Option(null) | |
optInteger: Option[Integer] = None | |
scala> useOptionInt(optInteger) | |
<console>:16: error: type mismatch; | |
found : Option[Integer] | |
required: Option[Int] | |
// implicit conversion still fails, because it happens before the call to Option | |
scala> useOptionInt(Option(i)) | |
java.lang.NullPointerException | |
at scala.Predef$.Integer2int(Predef.scala:392) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment