Matching Using case Classes
case classes are special classes that can be used in pattern matching with case expressions. A case class is concise and easy to create, and it exposes each of its constructor parameters as values. You can use case classes to create lightweight value objects or data holders with meaningful names for the class and its properties.
Suppose we want to receive and process stock-trading transactions. The messages for selling and buying might be accompanied by information such as the name of a stock and a quantity. It’s convenient to store this information in objects, but how would we pattern-match them? This is the purpose of case classes. These are classes that the pattern matcher readily recognizes and matches. Here’s ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access