Skip to content

Instantly share code, notes, and snippets.

@odwrotnie
Created February 1, 2012 14:37
Show Gist options
  • Save odwrotnie/1717340 to your computer and use it in GitHub Desktop.
Save odwrotnie/1717340 to your computer and use it in GitHub Desktop.
NotNull

Usage

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 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment