【发布时间】:2020-08-19 08:01:37
【问题描述】:
假设如下情况
case class MyCaseClass(x: Int) // some case clas with an Int parameter
val x0 = 17 // some given Int value
val a = MyCaseClass(x=18) // a case class object
// using pattern matching to find out if *a* is of type MyCaseClass and its parameter x equals x0+1
a match {
case MyCaseClass(x) if x == x0+1 => println("Found my case class with x==x0+1")
}
问题
如果可以跳过上面的 if 语句 if x == x0+1 并直接写
case MyCaseClass(x0+1) => println("Found my case class with x==x0+1")
不幸的是,这无法编译 - 但有什么方法可以实现这样的目标吗?
【问题讨论】:
标签: scala pattern-matching parameter-passing case-class