10 Representing unknown values

 

This chapter covers

  • Understanding how undefined values are used
  • Representing the absence of a value with the Nothing type
  • Dealing with values that exist but are unknown using the Missing type

Something important to deal with in any programming language is representing the absence of a value. For a long time, most mainstream programming languages, such as C/C++, Java, C#, Python, and Ruby, had a value called null or nil, which is what a variable would contain if it did not have any value. More accurately phrased: null or nil indicates that a variable is not bound to a concrete object.

When would this be useful? Let’s use Julia’s findfirst function as an example. It locates the first occurrence of a substring:

julia> findfirst("hello", "hello world")    #1
1:5
 
julia> findfirst("foo", "hello world")      #2

But how do you indicate that a substring cannot be found? Languages such as Java, C#, and Python would use the null or nil keywords to indicate this. However, it is not without reason its inventor, British computer scientist Tony Hoare, called the null pointer his billion-dollar mistake.

It makes it difficult to write safe code because any variable could be null at any given time. In programs written using languages supporting null, you need a lot of boilerplate code performing null checks. That is because it is unsafe to perform operations on null objects.

10.1 The nothing object

10.2 Using nothing in data structures

10.2.1 What is a parametric type?

10.2.2 Using union types to end the wagon train

10.3 Missing values

10.4 Not a number

10.5 Undefined data

10.6 Putting it all together

Summary