- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
case classes' copy method cannot override?
Sun, 2010-05-16, 00:40
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
Sun, 2010-05-16, 08:57
#2
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:
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
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