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

Another strike against case class inheritance

2 replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

Here is yet another peculiarity that results if you define a case class
that inherits from another case class. I don't think it has been
mentioned before:

scala> case class X(x: Int)
defined class X

scala> case class Y(y: Int) extends X(1)
defined class Y

scala> Y(2).copy()
res15: X = X(1)

Oops! Surely a copy of a Y should be a Y...

filed as https://lampsvn.epfl.ch/trac/scala/ticket/2349

fwiw, I'm hoping case class inheritance will just get deprecated in 2.8.

(and for those who missed previous discussions on the subject: by "case
class inheritance" I mean one case class inheriting from another case
class. no one is proposing disallowing case classes from having
non-case subclasses or superclasses)

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Another strike against case class inheritance

On Sat, Sep 12, 2009 at 06:02:35PM -0500, Seth Tisue wrote:
> fwiw, I'm hoping case class inheritance will just get deprecated in 2.8.

I see you aren't reading the commit logs with the religious fervor which
they warrant.

r18694 | extempore | 2009-09-11 08:23:05 -0700 (Fri, 11 Sep 2009) | 3 lines

Deprecated case classes inheriting from other case classes,
and updated all the tests which did so.

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: Another strike against case class inheritance


On Sun, Sep 13, 2009 at 01:02, Seth Tisue <seth [at] tisue [dot] net> wrote:

Here is yet another peculiarity that results if you define a case class
that inherits from another case class.  I don't think it has been
mentioned before:

scala> case class X(x: Int)
defined class X

scala> case class Y(y: Int) extends X(1)
defined class Y

scala> Y(2).copy()
res15: X = X(1)

Oops!  Surely a copy of a Y should be a Y...


This may be unexpected, but it is intended. the "copy" method is only created if no
other member named "copy" already exists in the class (or in a superclass).
We could instead generate overloaded copy methods whenever possible (it would
not be possible in your example), but we went for simplicity here.

For case-class inheritance you get a copy method only for the topmost case class,
for example

case class A(x: String)
case class B(y: Int) extends A(""+ y)
B(1).copy("foo")
// B(1).copy(2) // error, wrong parameter type for copy

Anyway, as Paul says, all this is obsolete now.

Cheers: Lukas

 

filed as https://lampsvn.epfl.ch/trac/scala/ticket/2349

fwiw, I'm hoping case class inheritance will just get deprecated in 2.8.

(and for those who missed previous discussions on the subject: by "case
class inheritance" I mean one case class inheriting from another case
class.  no one is proposing disallowing case classes from having
non-case subclasses or superclasses)

--
Seth Tisue / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

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