For a while I have been struggling to integrate scala with java methods that might return null. I came up with the following utility which helps a lot:
// produce an Option, nulls become None
object Maybe {
def apply[T](t:T) = if (t==null) None else Some(t)
}
Maybe(javaClass.getResultCouldBeNull()).map( result => doSomeWork(result) )
I have a few questions about this solution:
Scala's built-in Option is what you're setting out to reinvent.
In that case:
scala> val sOpt: Option[String] = Option(null)
sOpt: Option[String] = None