【问题标题】:Scala case class copy-method difference between 2.9 and 2.10Scala案例类复制方法2.9和2.10之间的区别
【发布时间】:2014-01-03 12:57:55
【问题描述】:

以下代码在 Scala 2.9.1 中编译:

scala> case class Foo(a: String)(val b: Int = 1)
defined class Foo

scala> val foo = Foo("some")(2)
foo: Foo = Foo(some)

scala> foo.copy("another")()
res1: Foo = Foo(another)

但在 2.10.3 中,我们收到以下错误:

scala> foo.copy("another")()
<console>:11: error: not enough arguments for method copy: (b: Int)Foo.
Unspecified value parameter b.
          foo.copy("another")()

有人能解释一下为什么会这样吗?而且我还想知道除了 foo.copy("another")(foo.b)

是否有一些聪明的方法可以做到这一点

【问题讨论】:

  • 貌似回归了,请举报here

标签: scala scala-2.10 case-class


【解决方案1】:

不幸的是,这是设计使然:https://issues.scala-lang.org/browse/SI-6068

像这样的案例类上的辅助参数块通常仅用于隐式。它们的用途有限,因为它们不参与模式匹配或(如您所见)复制操作。

【讨论】:

  • 来自编译器的更好的错误消息,或者甚至是Foo 定义处的警告会很好。
猜你喜欢
  • 2015-11-11
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 2015-01-09
  • 2011-01-19
  • 2012-08-06
  • 1970-01-01
  • 2015-12-11
相关资源
最近更新 更多