Match multiple cases classes in scala

Viewed 96877

I'm doing matching against some case classes and would like to handle two of the cases in the same way. Something like this:

abstract class Foo
case class A extends Foo
case class B(s:String) extends Foo
case class C(s:String) extends Foo


def matcher(l: Foo): String = {
  l match {
    case A() => "A"
    case B(sb) | C(sc) => "B"
    case _ => "default"
  }
}

But when I do this I get the error:

(fragment of test.scala):10: error: illegal variable in pattern alternative
    case B(sb) | C(sc) => "B"

I can get it working of I remove the parameters from the definition of B and C but how can I match with the params?

3 Answers
Related