I routinely use partial functions to factor out common clauses in exception handling. For example:
val commonHandler: PartialFunction[Throwable, String] = {
case ex: InvalidClassException => "ice"
}
val tried = try {
throw new Exception("boo")
}
catch commonHandler orElse {
case _:Throwable => "water"
}
println(tried) // water
Naturally, I expected that the match
keyword also expects a partial function and that I should be able to do something like this:
val commonMatcher: PartialFunction[Option[_], Boolean] = {
case Some(_) => true
case None => false
}
Some(8) match commonMatcher // Compilation error
What am I doing wrong?
The
match
is a keyword, not a method, and its syntax does not accept a partial function on its right side (see below). There however exists apipe
method (since 2.13), which, same asmap
orforeach
, accepts a partial function. You can therefore write:There was some discussion regarding this (see Pre SIP: Demote match keyword to a method) and there was a PR which made possible to use the
match
a bit more like a method (Change match syntax #7610), with a dot, but still the syntax is thematch
keyword needs to be followed by case clauses, see https://docs.scala-lang.org/scala3/reference/syntax.html:Compare this with
catch
syntax: