Chapter 6. Optional and Monads
In functional languages, there is a data type called the option type, also called the maybe type. Its purpose is to encapsulate a value, which is returned from a function. This option return type may indicate that there is no return value. The problem with returning a value such as zero or null is that these may be legitimate return values. With an option type, we can return a good value or indicate that nothing is returned.
In Java 8, the Optional
class has been added to serve as an option type. It is used to wrap an object and possesses a number of methods that can reflect a non-value and deal with these values. The calls allow us to handle null values and avoid the dreaded null pointer exception. The Optional
class also plays an important role in supporting fluent interfaces.
We will also examine the nature and use of monads. This is a technique to compose functions using a fluent style. This concept is present in functional programming languages...