Skip to main content

Pattern Matching

  • Chapter
  • First Online:
Beginning Scala 3

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 49.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 64.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10

  2. 2.

    https://docs.scala-lang.org/tour/extractor-objects.html#inner-main

  3. 3.

    ESME is the Enterprise Social Messaging Experiment (http://blog.esme.us).

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

    https://regexr.com/

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

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2022 The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

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

Publish with us

Policies and ethics