Appendix C. Options and boxes

If you have any experience with any other programming language, you’ll no doubt have seen code that does some guard-style operation, like this:

if (thing != null)
{
    thing.whatever();
}

The purpose is to prevent one from calling the whatever() method on someobject when it is null; otherwise the method would explode with a NullPointer-Exception at runtime because null is not a proper object instance. This style of programming can add a lot of noise to your code, and Scala has a solution for this: the Option type.

Scala abstracts the whole idea of a value being something or nothing, so rather than having to continually create guards within your code, you wrap your type with an Option and then, when you want to do ...

Get Lift in Action now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.