score:25

Accepted answer

Standard pattern-matching will always match on only exactly one case. You can get close to what you want by using the fact that patterns can be treated as partial functions (see the Language Specification, Section 8.5, Pattern Matching Anonymous Functions) and by defining your own matching operator, though:

class MatchAll[S](scrutinee : =>S) {
  def matchAll[R](patterns : PartialFunction[S,R]*) : Seq[R] = {
    val evald : S = scrutinee
    patterns.flatMap(_.lift(evald))
  }
}

implicit def anyToMatchAll[S](scrut : =>S) : MatchAll[S] = new MatchAll[S](scrut)

def testAll(x : Int) : Seq[String] = x matchAll (
  { case 2 => "two" },
  { case x if x % 2 == 0 => "even" },
  { case x if x % 2 == 1 => "neither" }
)

println(testAll(42).mkString(",")) // prints 'even'
println(testAll(2).mkString(","))  // prints 'two,even'
println(testAll(1).mkString(","))  // prints 'neither'

The syntax is slightly off the usual, but to me such a construction is still a witness to the power of Scala.

Your example is now written as:

// prints both 'foo' and 'bar'
"both" matchAll (
  { case "both" | "foo" => println("foo") },
  { case "both" | "bar" => println("bar") }
)

(Edit huynhjl pointed out that he gave a frighteningly similar answer to this question.)

score:0

One possible way could be:

val a = "both"

a match {
  case "foo" => println ("foo")   // Case 1
  case "bar" => println ("bar")   // Case 2
  case "both" => println ("foo"); println ("bar")
}

score:1

Just match twice:

val a = "both"

a match {
    case "both" | "foo" => println ("foo")   // Case 1
}
a match {
    case "both" | "bar" => println ("bar")   // Case 2
}

score:3

match executes one, and only one, of the cases, so you can't do this as an or in the match. You can, however, use a list and map/foreach:

val a = "both"
(a match {
  case "both" => List("foo", "bar")
  case x => List(x)
}) foreach(_ match {
  case "foo" => println("foo")
  case "bar" => println("bar")
})

And you're not duplicating any of the important code (in this case the printlns).

score:6

At risk of being Captain Obvious, in a case like this it would be simplest just to forget pattern matching and use if.

if (a == "both" || a == "foo") println("foo")
if (a == "both" || a == "bar") println("bar") 

If the repetition of a == worries you, you could instead write

if (Set("both", "foo")(a)) println("foo")
if (Set("both", "bar")(a)) println("bar")

using the fact that the apply method on Set does the same as contains, and is a bit shorter.


Related Query

More Query from same tag