24 Working with Option: For-comprehension

After reading this lesson, you will be able to

  • Chain optional values together using for-comprehension
  • Introduce conditions within for-comprehension constructs
  • Code using the most common operations defined on Option

In the previous lesson, you learned how to use the functions map, flatten, and flatMap to manipulate optional values. In this lesson, you are also going to discover a more readable and elegant way of combining instances of Option thanks to a new type of construct called for-comprehension. You’ll also see how to integrate Boolean conditions to control how values are chained together. Finally, you’ll discover other useful operations implemented for Option, such as isDefined, getOrElse,

Get Get Programming with Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.