25 Tuple and unapply

After reading this lesson, you’ll be able to

  • Group elements using tuples
  • Retrieve data from tuples
  • Extract information from an instance of a class using the unapply method

In the previous lessons, you discovered how to handle nullable values using Option. In this lesson, you’ll learn about tuples, one of the most basic data structures Scala offers to quickly group data in a given order. You’ll then combine what you have seen about tuples and the type Option to discuss the unapply method. The function unapply is complementary to apply: you use the apply function to create a class instance and unapply to extract information from it.

Pattern matching is one of the most powerful tools you can use. So far, you have seen that ...

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.