Possible Duplicate:
Using comparison operators in Scala’s pattern matching system
对于下面的方法,我收到一个错误:“'=>'预期但找到整数文字。”
是否无法检查 x 是否大于另一个数字,或者如果“> 2”匹配,是否有其他方法返回“大于 2”?
def describe(x: Any) = x match {
case 5 => "five"
case > 2 => "greater than 2"
}
最佳答案
尝试:
def describe(x: Any) = x match {
case 5 => "five"
case x: Int if (x > 2) => "greater than 2"
}
关于scala - scala中的模式匹配,检查数字是否大于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037852/