Optionals #
Java provides an Optional API for handling missing values with functional combinators.
Optional
is primarily intended for use as a method return type where there is a clear need to represent “no result,” and where usingnull
is likely to cause errors.
The combinators map
, filter
and flatMap
are useful for handling optional values,
but their use does not come naturally to programmers who are used to
conventional programming patterns involving null
checks.
After introducing the Optional
interface via unit tests
demonstrating the behaviour of the mentioned combinators,
we show how to transform traditional null
checks into idiomatic
use of optionals.
We will also discuss anti-patterns when programming with optionals
and how to avoid them.
Our discussion provides another example of how the functional combinators can be used
as powerful combining forms for building new programs from existing ones
while employing useful properties for reasoning about programs
as mentioned in the introduction to this tutorial.
More specifically, we will see how programs that would conventionally use
sequencing and conditional statements
can be expressed using expressions involving functional interfaces with optionals.
Presenting this combinatorial style of programming
by means of the relatively simple type of optionals
will serve as a bridge to combinatorial parsing
and can help consolidate and expand intuitions assiciated with map
, filter
and flatMap
that are independent of concrete types implementing these combinators.