我想知道如何使用多种类型的模式匹配。我有:
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()
}
所以我想写一些类似的东西:
x match {
case a @ (MyFirst | MySecond) => doSomething()
case _ => doSomethingElse()
}
我在一些教程中看到了类似的结构,但它给了我错误:
pattern type is incompatible with expected type;
[error] found : object MyFirst
[error] required: MyAbstract
那么有没有办法在 on case 子句中定义几种不同的类型?我认为它会使代码更漂亮。好像我会有 5 个一样,我会写 5 次相同的代码(调用 doSomething())。
提前致谢!
doSomething
情况,您都有一个共同的超类,为什么不匹配 case a : MyAbstract
那么...?
您缺少案例类的括号。不推荐使用没有参数列表的案例类。
尝试这个:
abstract class MyAbstract
case class MyFirst() extends MyAbstract
case class MySecond() extends MyAbstract
val x: MyAbstract = MyFirst()
x match {
case aOrB @ (MyFirst() | MySecond()) => doSomething(aOrB)
case _ => doSomethingElse()
}
如果您的案例类有太多参数并且不喜欢编写长 Foo(_,_,..)
模式,那么可能:
x match {
case aOrB @ (_:MyFirst | _:MySecond) => doSomething(aOrB)
case _ => doSomethingElse()
}
要不就:
x match {
case _:MyFirst | _:MySecond => doSomething(x) // just use x instead of aOrB
case _ => doSomethingElse(x)
}
但也许你只是想要单例对象?
abstract class MyAbstract
case object MyFirst extends MyAbstract
case object MySecond extends MyAbstract
val x: MyAbstract = MyFirst
x match {
case aOrB @ (MyFirst | MySecond) => doSomething()
case _ => doSomethingElse()
}
obj @ (_: MyFirst | _: MySecond)
吗?doSomething
调用中使用它的情况下,我需要obj
。在我的例子中,doSomething
的调用没有使用obj
,所以我不需要它。但无论如何,谢谢你的评论!