val x: String = "asdf"
val y: String = null
Then:
NotNull(x)("?")
asdf
NotNull(x.size)(-1)
4
NotNull(y)("?")
?
NotNull(y.size)(-1)
-1
object NotNull { | |
def apply[R](n: => R)(f: => R) = try { | |
if (n == null) throw new NullPointerException | |
n | |
} catch { case npe: NullPointerException => f } | |
} |