是否可以将匹配scala操作与变量一起使用?

时间:2011-08-22 14:39:07

标签: scala pattern-matching

  

可能重复:
  Why does pattern matching in Scala not work with variables?
  Pattern match for variable in scope (Scala)

例如我有代码

def equals(value1:String, value2:String) = value1 match {
 case value2 => true
 case _ => false
}

我找到了解决方法,但我不太喜欢语法

def equals(value1:String, value2:String) = value1 match {
 case v if v == value2 => true
 case _ => false
}

1 个答案:

答案 0 :(得分:7)

使用反引号括号值2:

case `value2` => true