Skip to content

Instantly share code, notes, and snippets.

@koeninger
Created September 15, 2017 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koeninger/a388f64b8e5c20e05358f593146dfa7c to your computer and use it in GitHub Desktop.
Save koeninger/a388f64b8e5c20e05358f593146dfa7c to your computer and use it in GitHub Desktop.
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