If you use this with functions that can return null, you can also define a null coalescing operator:
class NullOption[A <: AnyRef](a: A) { def |?|[B >: A](b: => B) = if (a ne null) a else b } implicit def null_handling[A <: AnyRef](a: A) = new NullOption(a)
which works just like javascript. (I used |?| Not to make a mistake in the logical boolean or, but you can use || if you want.) This works even better - almost like Option - if you add a conditional card (here called ? - select your favorite word or symbol):
class NullOption[A <: AnyRef](a: A) { def |?|[B >: A](b: => B) = if (a ne null) a else b def ?[C >: Null](f: A => C): C = if (a ne null) f(a) else null } implicit def null_handling[A <: AnyRef](a: A) = new NullOption(a)
Then you can
scala> val s: String = null s: String = null scala> val t: String = "foo" t: String = foo scala> s?(_.toUpperCase) |?| t |?| { println("I am not executed"); "bar" } res4: java.lang.String = "foo"
Of course, your type system will not help you remember that you need to handle no-data cases, but it can make handling with a null value a little more enjoyable (at least as long as there are no primitives; with primitives, returning zero -primitive, actually does not make sense).
source share