This page is no longer maintained — Please continue to the home page at www.scala-lang.org

case classes' copy method cannot override?

2 replies
Aydjen
Joined: 2009-08-21,
User offline. Last seen 1 year 28 weeks ago.

Hi,

I was hoping to write a super trait for a couple of case classes and re-use the auto-generated copy method. As an example, look at this REPL session:

-----
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait Medium {
| def content: String
| def copy(s: String = content): Medium
| }
defined trait Medium

scala> case class Paper(content: String) extends Medium
:6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Medium is not defined
case class Paper(content: String) extends Medium
^
-----

It seems that the auto-generated copy method can not override the abstract one in the trait, even though their signatures seem to be exact the same. Am I doing something wrong here or is that just not possible?

I've also tried the following:

-----
scala> trait Medium[T <: Medium[T]] { this: T =>
| def content: String
| def copy(s: String = content): T
| }
defined trait Medium

scala> case class Paper(content: String) extends Medium[Paper]
:6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Paper is not defined
case class Paper(content: String) extends Medium[Paper] {
^
-----

Thanks for advice
Andreas

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: case classes' copy method cannot override?

hmmm, don't know. i thought that you might need to adjust the return type, but still no luck:

scala> trait Medium[ Impl ] {
| def content: String
| def copy(s: String = content): Impl
| }
defined trait Medium

scala> case class Paper(content: String) extends Medium[ Paper ]
:11: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Paper is not defined
case class Paper(content: String) extends Medium[ Paper ]
^

Am 16.05.2010 um 00:40 schrieb Andreas Flierl:

> Hi,
>
> I was hoping to write a super trait for a couple of case classes and re-use the auto-generated copy method. As an example, look at this REPL session:
>
>
> -----
> Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait Medium {
> | def content: String
> | def copy(s: String = content): Medium
> | }
> defined trait Medium
>
> scala> case class Paper(content: String) extends Medium
> :6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Medium is not defined
> case class Paper(content: String) extends Medium
> ^
> -----
>
>
> It seems that the auto-generated copy method can not override the abstract one in the trait, even though their signatures seem to be exact the same. Am I doing something wrong here or is that just not possible?
>
> I've also tried the following:
>
>
> -----
> scala> trait Medium[T <: Medium[T]] { this: T =>
> | def content: String
> | def copy(s: String = content): T
> | }
> defined trait Medium
>
> scala> case class Paper(content: String) extends Medium[Paper]
> :6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Paper is not defined
> case class Paper(content: String) extends Medium[Paper] {
> ^
> -----
>
> Thanks for advice
> Andreas

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: case classes' copy method cannot override?
This is not supported.
The copy methods are only generated if there is no member named"copy" in the class, directly defined or inherited.
Lukas



On Sun, May 16, 2010 at 01:40, Andreas Flierl <andreas [at] flierl [dot] eu> wrote:
Hi,

I was hoping to write a super trait for a couple of case classes and re-use the auto-generated copy method. As an example, look at this REPL session:


-----
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait Medium {
    | def content: String
    | def copy(s: String = content): Medium
    | }
defined trait Medium

scala> case class Paper(content: String) extends Medium
<console>:6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Medium is not defined
      case class Paper(content: String) extends Medium
                 ^
-----


It seems that the auto-generated copy method can not override the abstract one in the trait, even though their signatures seem to be exact the same. Am I doing something wrong here or is that just not possible?

I've also tried the following:


-----
scala> trait Medium[T <: Medium[T]] { this: T =>
    | def content: String
    | def copy(s: String = content): T
    | }
defined trait Medium

scala> case class Paper(content: String) extends Medium[Paper]
<console>:6: error: class Paper needs to be abstract, since method copy in trait Medium of type (s: String)Paper is not defined
      case class Paper(content: String) extends Medium[Paper] {
                 ^
-----

Thanks for advice
Andreas

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland