Swipe to navigate through the chapters of this book
So far, we’ve explored some of the basic functional cornerstones of Scala: immutable data types and the passing of functions as parameters. The third cornerstone of functional programming is pattern matching. 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, we’re going to explore the basics of pattern matching. Then we’re going to see how Scala’s case classes bridge between object-oriented data encapsulation and function decomposition. Next, we’ll see how Scala’s pattern-matching constructs become functions that can be passed around and composed. Let's look at the simple example first.
Please log in to get access to this content
To get access to this content you need the following product:
ESME is the Enterprise Social Messaging Experiment ( http://blog.esme.us ).
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.
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.
- Pattern Matching
- Sequence number
- Chapter number
- Chapter 5