Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Absence of result

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content