The block with the case statements defines a function which maps integers to strings. The match keyword provides a convenient way of applying a function (like the pattern matching function above) to an object.

Here is a second example which matches a value against patterns of different types:

The first case matches if x refers to the integer value 1. The second case matches if x is equal to the string "two". The third case consists of a typed pattern; it matches against any integer and binds the selector value x to the variable y of type integer.

Scala’s pattern matching statement is most useful for matching on algebraic types expressed via case classes.
Scala also allows the definition of patterns independently of case classes, using unapply methods in extractor objects.