未定义Scala案例类的备用构造函数:没有足够的方法参数
发布时间:2020-12-16 09:07:49 所属栏目:安全 来源:网络整理
导读:我无法弄清楚为什么这不起作用…在编译期间我收到以下错误: [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,l
我无法弄清楚为什么这不起作用…在编译期间我收到以下错误:
[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,2,9)) 对于这个案例类…请注意备用构造函数的定义: case class GPLayer(id: Long,attachments: List[GPAttachment]) { def this(id: Long,created: Long) = this(id,glimpleId,layerOrder,created,List[GPAttachment]()) } 解决方法GPLayer(1,9) 和写作一样 GPLayer.apply(1,9) 因此,您应该在配套对象GPLayer中定义另一种apply方法,而不是定义替代构造函数. case class GPLayer(id: Long,attachments: List[GPAttachment]) object GPLayer { def apply(id: Long,created: Long) = GPLayer(id,List[GPAttachment]()) } 如果要改为调用altnernative构造函数,则必须添加new-keyword: new GPLayer(1,9) 编辑:正如Nicolas Cailloux所提到的,您的替代构造函数实际上只是为成员附件提供默认值,因此最佳解决方案实际上是不引入新方法,而是指定此默认值,如下所示: case class GPLayer(id: Long,attachments: List[GPAttachment] = Nil) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |