O'Reilly logo

Lift in Action: The Simply Functional Web Framework for Scala by Timothy Perrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required