Scala 案例 class:复制不等于/哈希码成员

Scala case class : copy non equal/ hascode members

我想在案例上使用文案类。但是 scala 坚持要我指定在第二个 paranthsis 中声明的所有属性。

示例:

package academic.classes.casec.copy

object TstClz {
  val f = DataMe("SD") ( "B")
  val x = f.copy("x 4")()//Error : not enough arguments for method copy: (b: String, c: String)academic.classes.casec.copy.DataMe. Unspecified value parameters b, c.
}

case class DataMe (a : String, a1 : String = "a1")(b:String, c: String = ""){}

这是功能还是错误?我该怎么做才能让它按照我想要的方式工作(从正在制作副本的实例中获取 b 和 c 的值?

这似乎有效:

object TstClz {
  val f = DataMe("SD") ( "B")
  val x = f.copy("x 4")(b = f.b, c = f.c)
}

case class DataMe (a : String, a1 : String = "a1")(val b:String, val c: String = "") {}

仅添加 b = f.b, c = f.c 是不够的,seconds 列表中的参数似乎没有默认定义 case class 值,并且没有用 val 标记它们,即使是简单的 f.b 访问将失败。此外,copy 似乎被定义为返回一个带有两个参数但没有默认值的函数。

注意:我已经用 2.11.8 和 2.12.0 测试过了。我认为他的回答中引用的 the commit fxlae 并没有完全恢复行为,因为在提交中有这个来源评论:

Copy only has defaults on the first parameter list, as of SI-5009.

(提交标记为存在于 2.12.0 中)。

正如您在评论中所写,如果愿意,您可以添加自己的复制实现,以便所有 val 都有其默认值:

def copy(a: String = a, a1: String = a1, b: String = b, c: String = c): DataMe = {
  DataMe(a, a1)(b, c)
}

我认为这不可能。在 this 错误报告中,您可以找到以下语句:

Martin says: case class ness is only bestowed on the first argument list

the rest should not be copied.

后来有一个"bug fix" which made copy to return at least a function representing the second parameter list, but that behavior was reverted in this commit