[Scala 2.9.1]
I am creating a function that takes an Option[Int] as one of its parameters, as this parameter maybe null...
Here is what is happening...
scala> def nullTest(n:Option[Int]) = n.getOrElse(0)
nullTest: (n: Option[Int])Int
scala> nullTest(Option(null))
<console>:9: error: type mismatch;
found : Null(null)
required: Int
Note that implicit conversions are not applicable because they are ambiguous:
both method Integer2intNullConflict in class LowPriorityImplicits of type (x: Null)Int
and method Integer2int in object Predef of type (x: java.lang.Integer)Int
are possible conversion functions from Null(null) to Int
nullTest(Option(null))
So how can i create a Option[Int] parameter that maybe null?
FYI, This does work using a String.
IE.
scala> def nullTest(n:Option[String]) = n.getOrElse("I got Nothing")
nullTest: (n: Option[String])String
scala> nullTest(Option(null))
res20: String = I got Nothing