The thing you're asking about is called pattern matching. A pattern match block can be used with the match
keyword, or it can be used to define either a function or a partial function, depending on the context.
Your second example uses extractors. Scala's evaluation of m match { case A(x) => }
involves calling A.unapply(m)
. The companion object of a case class is an extractor (using case classes is more common than actually writing an unapply
method).
The Scala Language Specification is a tough read, but sometimes it's worth trying to take a look at, especially if you're looking for formalism. Chapter 8 is about pattern matching. Section 8.4 introduces the idea of using if
in a case clause like in your first example.