I'm wondering how can I use multiple type pattern matching. I have:
abstract class MyAbstract
case class MyFirst extends MyAbstract
case class MySecond extends MyAbstract
case class MyThird extends MyAbstract // shouldn't be matched and shouldn't call doSomething()
val x: MyAbstract = MyFirst
x match {
case a: MyFirst => doSomething()
case b: MySecond => doSomething()
case _ => doSomethingElse()
}
So I'd like to write something like:
x match {
case a @ (MyFirst | MySecond) => doSomething()
case _ => doSomethingElse()
}
I saw similar construction in some tutorial, but it gives me error:
pattern type is incompatible with expected type;
[error] found : object MyFirst
[error] required: MyAbstract
So is there a way to define few different types in on case clause? I think it would make code prettier. As if I will have 5 of them, I will write same code 5 times (calling doSomething()).
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…