문제

Following code compile in 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)

but in 2.10.3 we get following error:

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

Can someone explain why this is changed? And I also wanted to know if there is some clever way to do this, other than foo.copy("another")(foo.b)

도움이 되었습니까?

해결책

This is, unfortunately, by design: https://issues.scala-lang.org/browse/SI-6068

Auxiliary param blocks on case classes like this are generally only used for implicits. They're otherwise of limited use as they don't participate in pattern matching or (as you've seen) in copy operations.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top