为什么Scala编译器说copy不是我的case类的成员?

为什么Scala编译器说copy不是我的case类的成员?,scala,copy,case-class,Scala,Copy,Case Class,首先,它在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

首先,它在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]
case类有属性,所以应该有一个
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]
这是为什么?我如何在case类方法中使用
copy
?还是说
copy
是编译器在声明我的方法后添加的东西

我应该这么做吗

case class JsVar(varName: String, andThen: String*) extends JsExp {
  // ...
  def -&(right: String) = JsVar(varName, (right :: andThen.toList.reverse).reverse :_*)
}

规范对此没有提及,但这实际上是意料之中的。
copy
方法取决于默认参数,重复参数(varargs)不允许使用默认参数:

不允许定义任何违约 参数节中的参数 重复参数

(Scala参考,第4.6.2节-重复参数)

scala>def(xs:Int*)=xs
f:(xs:Int*)Int*
scala>def(xs:Int*=List(1,2,3))=xs
:24:错误:类型不匹配;
找到:列表[Int]
必填项:Int*
def(xs:Int*=List(1,2,3))=xs
^
:24:错误:带有“*”参数的参数节不允许有默认参数
def(xs:Int*=List(1,2,3))=xs
^

这似乎与vararg参数有关。。。如果不检查bug跟踪器或规范,这感觉就像一个bug。JsExp已经是一个案例类了吗?如果是这样,这可能与case类继承的已知问题有关。不,这是一个扩展了两个简单特性的(相当大的)特性。简化的问题,在2.9.0.1上:
scala>case类Foo(xs:Int*){def bar=copy(xs=List(1,2,3):*)}:8:错误:找不到:值copy def bar=copy(xs=List(1,2,3):*}
Ok,这是有道理的。谢谢但是,为什么不能为重复的参数设置默认值呢?有什么魔法吗?我注意到
案例类A(b:String*)
A(“A”,“b”)
A(List(“A”,“b”):*)
有不同的签名。@pr1001没有线索。您可能希望阅读有关默认参数和命名参数的SID(SIP?),因为它可能会说明限制的原因。
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
           ^