38 Either

After reading this lesson, you will be able to

  • Define a value that can have one of two possible types, called Either
  • Decompose it using pattern matching
  • Transform its content using the map and flatMap functions
  • Chain multiple instances of Either using for-comprehension

In the previous lesson, you mastered the operations you can perform on a Map. In this lesson, you’ll discover a new Scala type called Either. You can use it to represent a value with one of two possible types. You’ll learn about its structure and how to define an instance for it. You’ll use pattern matching to handle all its possible implementations. You’ll transform its values using the map and flatMap function, and you’ll chain multiple values using for-comprehension ...

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.