pr1*_*001 5 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 :_*)
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到以下编译器错误:
[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]
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
为什么这样,我如何copy
在我的case类方法中使用?或者是copy
在声明我的方法之后编译器添加的想法?
我应该这样做吗?
case class JsVar(varName: String, andThen: String*) extends JsExp {
// ...
def -&(right: String) = JsVar(varName, (right :: andThen.toList.reverse).reverse :_*)
}
Run Code Online (Sandbox Code Playgroud)
规范在这方面没有提及,但这实际上是预期的.该copy
方法取决于默认参数,并且重复参数(varargs)不允许使用默认参数:
不允许使用重复参数在参数部分中定义任何默认参数.
(Scala参考,第4.6.2节 - 重复参数)
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
^
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1411 次 |
最近记录: |