scala中的模式匹配,检查数字是否大于

时间:2012-10-23 19:33:40

标签: scala

  

可能重复:
  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"
  }

1 个答案:

答案 0 :(得分:8)

尝试:

def describe(x: Any) = x match {
  case 5 => "five"
  case x: Int if (x > 2) => "greater than 2"
}
相关问题