O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required