39 Working with Either

 

After reading this lesson, you will be able to

  • Retrieve a value wrapped in the left or right side for Either
  • Check if an instance is its left or right projection
  • Inquire if its value respects a given predicate

In the previous lesson, you learned the structure of Either and its basic operations. In this lesson, you’ll learn about other useful methods the class Either has to offer. They are very similar to the one you saw for Option. They do not share an interface, but this is a happy consequence of the consistent design and style of the Scala collections. You’ll discover how to retrieve a value defined for its left or right side. You’ll also inquire about its properties to check if a given instance is of type Left or Right and if it contains a value with specific features. You will use these operations to determine if your library has accepted a book-loaning request in the capstone.

39.1 Retrieving an Either value

In the previous lesson, you implemented a program to mark exams that produce a value of type Either[String, Pass] in which its left side represents a failure with a message for the student and its right side a pass with the obtained score.

39.2 Properties of an Either value

Summary

Answers to quick checks