我需要一个具体示例来说明如何在不可变 _case_ class 的主构造函数中定义局部参数
I need a specific example of how to define a local parameter in the primary constructor of an immutable _case_ class
我有普通的 Scala class 我想重构成为一个不可变的案例 class。因为我需要 class 在 Set
操作中表现良好,所以我希望所有 Scala 编译器自动生成的方法都在 class 案例中提供。 IOW,我想避免编写这些不同的方法; equals
、hashCode
、toString
等,因为这很容易出错。我需要为大量 classes 执行此操作,因此我需要一个通用解决方案,而不仅仅是一个特定的解决方案异常快速修复或 hack。
这是我正在使用的class:
class Node(val identity: String, childrenArg: List[Node], customNodeArg: CustomNode) {
val children: List[Node] = childrenArg
val customNode: CustomNode = customNodeArg
}
如您所见,class 的构造函数具有三个参数。第一个 identity
是只读的 属性。剩下的两个,childrenArg
和customNodeArg
,只是一个普通的方法参数;也就是说,它们仅在实例构造期间存在,然后在 class 构造函数执行完成后从 class 实例中完全消失(除非另外捕获)。
我第一次天真的尝试将其转换为不可变的情况 class 是这样的(只是从第一个参数中删除 val
):
class Node(identity: String, childrenArg: List[Node], customNodeArg: CustomNode) {
val children: List[Node] = childrenArg
val customNode: CustomNode = customNodeArg
}
然而,这导致了 childrenArg
和 customNodeArg
参数现在被提升为(只读)属性(而不是将它们保留为普通方法参数)的不良影响.这进一步产生了不良影响,将它们包含在编译器生成的 equals
和 hashCode
实现中。
如何标记不可变大小写 class 的构造函数参数 childrenArg
和 customNodeArg
以便 identity
是唯一的只读 属性 class?
关于这方面的任何指导;非常感谢答案、网站讨论链接等。
案例class参数默认为vals,但您可以设置为vars。
case class Node(identity: String, var childrenArg: List[Node], var customNodeArg: CustomNode)
将它们设为 vars 会自动为您提供 getter 和 setter
第二个参数列表似乎可以解决问题:
scala> trait CustomNode
defined trait CustomNode
scala> case class Node(identity: String)(childrenArg: List[Node], customNodeArg: CustomNode)
defined class Node
scala> val n = Node("id")(Nil, null)
n: Node = Node(id)
scala> n.identity
res0: String = id
scala> n.getClass.getDeclaredFields.map(_.getName)
res1: Array[String] = Array(identity)
我有普通的 Scala class 我想重构成为一个不可变的案例 class。因为我需要 class 在 Set
操作中表现良好,所以我希望所有 Scala 编译器自动生成的方法都在 class 案例中提供。 IOW,我想避免编写这些不同的方法; equals
、hashCode
、toString
等,因为这很容易出错。我需要为大量 classes 执行此操作,因此我需要一个通用解决方案,而不仅仅是一个特定的解决方案异常快速修复或 hack。
这是我正在使用的class:
class Node(val identity: String, childrenArg: List[Node], customNodeArg: CustomNode) {
val children: List[Node] = childrenArg
val customNode: CustomNode = customNodeArg
}
如您所见,class 的构造函数具有三个参数。第一个 identity
是只读的 属性。剩下的两个,childrenArg
和customNodeArg
,只是一个普通的方法参数;也就是说,它们仅在实例构造期间存在,然后在 class 构造函数执行完成后从 class 实例中完全消失(除非另外捕获)。
我第一次天真的尝试将其转换为不可变的情况 class 是这样的(只是从第一个参数中删除 val
):
class Node(identity: String, childrenArg: List[Node], customNodeArg: CustomNode) {
val children: List[Node] = childrenArg
val customNode: CustomNode = customNodeArg
}
然而,这导致了 childrenArg
和 customNodeArg
参数现在被提升为(只读)属性(而不是将它们保留为普通方法参数)的不良影响.这进一步产生了不良影响,将它们包含在编译器生成的 equals
和 hashCode
实现中。
如何标记不可变大小写 class 的构造函数参数 childrenArg
和 customNodeArg
以便 identity
是唯一的只读 属性 class?
关于这方面的任何指导;非常感谢答案、网站讨论链接等。
案例class参数默认为vals,但您可以设置为vars。
case class Node(identity: String, var childrenArg: List[Node], var customNodeArg: CustomNode)
将它们设为 vars 会自动为您提供 getter 和 setter
第二个参数列表似乎可以解决问题:
scala> trait CustomNode
defined trait CustomNode
scala> case class Node(identity: String)(childrenArg: List[Node], customNodeArg: CustomNode)
defined class Node
scala> val n = Node("id")(Nil, null)
n: Node = Node(id)
scala> n.identity
res0: String = id
scala> n.getClass.getDeclaredFields.map(_.getName)
res1: Array[String] = Array(identity)