- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Another strike against case class inheritance
Sun, 2009-09-13, 00:02
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)
Tue, 2009-09-22, 11:47
#2
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/
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.