为什么 Scala 编译器说 copy 不是我的 case 类的成员?
首先,这是在 Scala 2.8 中,所以它应该在那里! =)
我正在处理 Lift 的 Javascript 对象,我想要以下内容:
case class JsVar(varName: String, andThen: String*) extends JsExp {
// ...
def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
}
不幸的是,我收到以下编译器错误:
[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: not found: value copy
[error] def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]
案例类具有属性,因此应该有一个 copy
方法,对吧?
如果我尝试 this.copy
,我几乎会得到相同的错误:
[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: value copy is not a member of net.liftweb.http.js.JE.JsVar
[error] def -&(right: String) = this.copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]
为什么会这样以及如何在我的案例类方法中使用 copy
?或者 copy
是编译器在声明我的方法后添加的内容吗?
我应该这样做吗?
case class JsVar(varName: String, andThen: String*) extends JsExp {
// ...
def -&(right: String) = JsVar(varName, (right :: andThen.toList.reverse).reverse :_*)
}
First, this is in Scala 2.8, so it should be there! =)
I'm working on Lift's Javascript objects and I want to have the following:
case class JsVar(varName: String, andThen: String*) extends JsExp {
// ...
def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
}
Unfortunately, I get the following compiler error:
[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: not found: value copy
[error] def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]
The case class has properties, so there should be a copy
method, right?
If I try this.copy
I get practically the same error:
[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: value copy is not a member of net.liftweb.http.js.JE.JsVar
[error] def -&(right: String) = this.copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]
Why is this and how can I use copy
in my case class method? Or is the idea that copy
is something the compiler adds after declaring my methods?
Should I just do this?
case class JsVar(varName: String, andThen: String*) extends JsExp {
// ...
def -&(right: String) = JsVar(varName, (right :: andThen.toList.reverse).reverse :_*)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规范对此没有提及,但这实际上是预期的。
copy
方法依赖于默认参数,默认参数不允许重复参数(varargs):(Scala 参考,第 4.6.2 节 - 重复参数)
The specification is silent on this regard, but this is actually expected. The
copy
method depends on default parameters, and default parameters are not allowed for repeated paramters (varargs):(Scala Reference, section 4.6.2 - Repeated Parameters)