Why does the Scala compiler say that copy is not a member of my case class?
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开发者_Go百科.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 :_*)
}
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):
It is not allowed to define any default arguments in a parameter section with a repeated parameter.
(Scala Reference, section 4.6.2 - Repeated Parameters)
scala> def f(xs: Int*) = xs
f: (xs: Int*)Int*
scala> def f(xs: Int* = List(1, 2, 3)) = xs
<console>:24: error: type mismatch;
found : List[Int]
required: Int*
def f(xs: Int* = List(1, 2, 3)) = xs
^
<console>:24: error: a parameter section with a `*'-parameter is not allowed to have default arguments
def f(xs: Int* = List(1, 2, 3)) = xs
^
精彩评论