说:
case class point(x: Double, y: Double)
有办法吗?
def test(p: point) = {
val point(x, y) = p // extract x,y from point. I want to make this automatic
x + y
}
简明扼要地说:
def test(point(x, y)) = x + y
这当然是无效的语法。
有没有办法让函数自动从案例类中提取值?
答案 0 :(得分:0)
你可以这样写:
def test(p: point) = p match{case point(x,y)=>x+y}
答案 1 :(得分:0)
<强>解决方案:强>
case class point(x: Double, y: Double) {
def sum = x + y // OMG IT WORKS!
}