Scala案例类上的替代构造函数未定义:方法的参数不足

Scala案例类上的替代构造函数未定义:方法的参数不足,scala,Scala,我不明白为什么这不起作用。。。在编译过程中,我遇到以下错误: [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, atta

我不明白为什么这不起作用。。。在编译过程中,我遇到以下错误:

[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.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
-关键字:

new GPLayer(1, 42, 1, 9)

编辑:如前所述,您的替代构造函数实际上只是为成员
附件提供默认值,因此最好的解决方案实际上是不引入新方法,而是按如下方式指定此默认值:

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())

哦,天哪。谈论对自己的代码视而不见。非常感谢。由于问题中的代码实际上只是指定了一个默认参数,因此这比添加进一步的构造函数/应用方法要干净得多。我在我的回答中加入了这个信息(给你信用),但是如果你不喜欢我参与你的回答,我会删除它;如果是这样的话,只需在我的用户名上添加一条评论,我就会得到通知。在任何情况下:好的附录+1没有问题,因为你的答案有更多的点,更明显!
case class GPLayer(id: Long, glimpleId: Long, layerOrder: Int, created: Long, attachments: List[GPAttachment] = List())