I need a specific example of how to define a local parameter in the primary constructor of an immutable _case_ class

纵饮孤独 提交于 2019-12-06 15:04:08

A second parameter list seems to do the trick:

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)

Case class parameters are vals by default, but you can set them to vars.

case class Node(identity: String, var childrenArg: List[Node], var customNodeArg: CustomNode)

Making them vars gives you getters and setters automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!