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

发布于 2024-11-19 09:03:26 字数 1242 浏览 1 评论 0原文

首先,这是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-11-26 09:03:26

规范对此没有提及,但这实际上是预期的。 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
           ^

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
           ^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文