方法参数中的Scala模式匹配?

方法参数中的Scala模式匹配?,scala,Scala,如何在方法的参数中拆分case类 scala> case class f(a:Int,b:Int) defined class f scala> def z((a,b):f) = a + b <console>:1: error: identifier expected but '(' found. def z((a,b):f) = a + b ^ 或者有更惯用的方法吗?如果您的case类像这样简单,您可以使用@XavierGuihot在注释中建议的方法

如何在方法的参数中拆分case类

scala> case class f(a:Int,b:Int)

defined class f

scala> def z((a,b):f) = a + b
<console>:1: error: identifier expected but '(' found.
def z((a,b):f) = a + b
      ^

或者有更惯用的方法吗?

如果您的case类像这样简单,您可以使用@XavierGuihot在注释中建议的方法。否则,您需要模式匹配。但是,有两种方法可以使用它:一种是使用
match
关键字,另一种是使用部分函数语法:

val z: f => Int = {
  case f(a, b) => a + b
}

只要
defz(r:f)=r.a+r.b
ohhhhhhhhhhhhhh在哈斯凯尔,你需要镜头。好吧,这是有道理的。
val z: f => Int = {
  case f(a, b) => a + b
}