我无法弄清楚为什么这不起作用...在编译期间,我会收到以下错误:
[error] /Users/zbeckman/Projects/Glimpulse/Server-2/project/glimpulse-server/app/service/GPGlimpleService.scala:17: not enough arguments for method apply: (id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[models.GPAttachment])models.GPLayer in object GPLayer.
[error] Unspecified value parameter attachments.
[error] private val layer1: List[GPLayer] = List(GPLayer(1, 42, 1, 9), GPLayer(2, 42, 2, 9))
对于此情况类别...请注意替代构造函数的定义:
case class GPLayer(id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[GPAttachment]) {
def this(id: Long, glimpleId: Long, layerOrder: Int, created: Long) = this(id, glimpleId, layerOrder, created, List[GPAttachment]())
}
GPLayer(1, 42, 1, 9)
与写作相同
GPLayer.apply(1, 42, 1, 9)
因此,您应该在伴随对象GPLayer
中定义替代的apply
方法,而不是定义替代构造函数。
case class GPLayer(id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[GPAttachment])
object GPLayer {
def apply(id: Long, glimpleId: Long, layerOrder: Int, created: Long) = GPLayer(id, glimpleId, layerOrder, created, List[GPAttachment]())
}
如果要调用altnernative构造函数,则必须添加 new
-keyword:
new GPLayer(1, 42, 1, 9)
编辑:正如Nicolas Cailloux所述,您的替代构造函数实际上只是为成员attachments
提供默认值,因此最好的解决方案实际上是不引入新方法,但要指定此方法默认值如下:
case class GPLayer(id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[GPAttachment] = Nil)
请注意,在您的情况下,您可以为最后一个参数提供默认值:
case class GPLayer(id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[GPAttachment] = List())