22 Option
After reading this lesson, you will be able to
- Represent a nullable value using
Option
- Use pattern matching on instances of the type
Option
Now that you’ve mastered higher order functions, you’ll learn about the type Option
. Using null
to represent nullable or missing values in Scala is an antipattern; use the type Option
instead. The type Option
ensures that you deal with both the presence or the absence of an element. Thanks to the Option
type, you can make your system safer by avoiding nasty NullPointerException
s at runtime. Your code will also be cleaner as you will no longer preventively check for null
values; you will be able to clearly mark nullable values and act accordingly only when effectively needed. The concept of an ...
Get Get Programming with Scala 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.