Abstract
Pattern matching provides a powerful tool for declaring business logic in a concise and maintainable way. Scala blends traditional functional programming pattern matching with object-oriented concepts to provide a very powerful mechanism for writing programs. In this chapter, you’re going to explore the basics of pattern matching. Then you’re going to see how Scala’s case classes bridge object-oriented data encapsulation and function decomposition. Next, you’ll see how Scala’s pattern-matching constructs become functions that can be passed around and composed. Let's look at a simple example first.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
- 2.
- 3.
ESME is the Enterprise Social Messaging Experiment (http://blog.esme.us).
- 4.
This code will not compile without the rest of the ESME code, but it serves as an illustration of using pattern matching as an alternative to XML configuration files or annotations.
- 5.
- 6.
Here is where a unity ped language such as Ruby or Python has a material advantage over a static language such as Java. In Ruby, you don’t need all the boilerplate and the class hierarchy is not fixed at the time the OCarVisitor interface is defined.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2022 The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature
About this chapter
Cite this chapter
Pollak, D., Layka, V., Sacco, A. (2022). Pattern Matching. In: Beginning Scala 3. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-7422-4_5
Download citation
DOI: https://doi.org/10.1007/978-1-4842-7422-4_5
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-7421-7
Online ISBN: 978-1-4842-7422-4
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)