Question

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)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top