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

Pattern Variables and Constants in case Expressions

You already saw how to define placeholder vals for what you’re matching, like lat and long when matching tuples. These are pattern variables. However, you have to be careful when defining them. By convention, Scala expects the pattern variables to start with a lowercase letter and constants to be capitalized.

If you use a capitalized name, it will look for a constant in the scope; however, if you use a noncapitalized name, it will merely assume it’s a pattern variable—any variable or constant with the same noncapitalized name in the scope will be ignored. In the next code, we’re defining a pattern variable with the same name as a field, but the code will not give us the desired result—the pattern ...

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