6 Dealing with optional data
- The
null
reference or “the billion dollar mistake” - Alternatives to
null
references - Developing an
Option
data type for optional data - Applying functions to optional values
- Composing optional values
Representing optional data has always been a problem in computer programs. The concept of optional data is simple. In everyday life, it’s easy to represent the absence of something when this something is stored in a container—whatever it is, it can be represented by an empty container. For example, an absence of apples can be represented by an empty apple basket. The absence of gasoline in a car can be visualized as an empty gas tank. But representing the absence of data in computer programs is a little more difficult.
Most data is represented by a reference pointing to it, so the most obvious way to represent the absence of data is to use a pointer to nothing. That’s what we call a null pointer. In Kotlin, a reference is a pointer to a value.