August 2018
Intermediate to advanced
380 pages
10h 2m
English
Consider a situation where we have a function that is supposed to make a database query. Specifically, we have users in our database, and we want a function to retrieve a user by their ID. Now, what will happen when there is no user with a given ID in the database? Consider the following:
def getUser(id: Int): User = if (Set(1, 2, 3).contains(id)) User(s"User-$id") else null
The solution of imperative languages is a null returned from the function. In Chapter 3, Functional Data Structures, we saw how this is dangerous. The compiler doesn't know that null can be returned from the function. More precisely, it doesn't even know this is a possibility. The compiler allows for functions returning null and does not warn us about ...