6 Dealing with optional data

 

In this chapter

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

    6.1 Problems with the null pointer

    6.2 How Kotlin handles null references

    6.3 Alternatives to null references

    6.4 Using the Option type

    6.4.1 Getting a value from an Option

    6.4.2 Applying functions to optional values

    6.4.3 Dealing with Option composition

    6.4.4 Option use cases

    6.4.5 Other ways to combine options

    6.4.6 Composing List with Option

    6.4.7 Using Option and when to do so

    Summary