I have some Scala code that needs to call a Java API
The Java API takes arguments that may be null. My Scala, of course, uses Option
.
For example, let's say I have a Java object constructor Foo(Integer)
where the Integer
may be null
. I want to call it given a Scala bar: Option[Int]
.
I tried this
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
val foo = Foo( bar.getOrElse(null) )
But got this compile error
Error:(335, 44) type mismatch;
found : Any
required: Integer
bar.getOrElse(null),
What is the correct idiom for doing this?
No sooner do I post the question, than I spot the answer in the related list (sorry)
Here's a solution
Kind of clunky. Anyone have anything better?