x and y are extracted from the tuple:
To ignore a value use _:
To unpack an extractor:
Note that tuples have a maximum length of 22, and thus ._1 through ._22 will work (assuming the tuple is at least that size).
Tuple extractors may be used to provide symbolic arguments for literal functions:
Case Class Extractors
A case class is a class with a lot of standard boilerplate code automatically included. One benefit of this is that Scala makes it easy to use extractors with case classes.
At this juncture, both n and a are vals in the program and can be accessed as such: they are said to have been 'extracted' from p. Continuing:
Here we see two important things:
• Extraction can happen at 'deep' levels: properties of nested objects can be extracted.
•Not all elements need to be extracted. The wildcard _ character indicates that that particular value can be anything, and is ignored. No val is created.
In particular, this can make matching over collections easy:
Here, we have code that uses the extractor to explicitly check that person is a Person object and immediately pull out the variables that we care about: n and a.