可能重复:
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"
}
答案 0 :(得分:8)
尝试:
def describe(x: Any) = x match {
case 5 => "five"
case x: Int if (x > 2) => "greater than 2"
}