Scala Wart: Abstract/non-final case classes
See original GitHub issueOpening this issue, as suggested by Martin, to provide a place to discuss the individual warts brought up in the blog post Warts of the Scala Programming Language and the possibility of mitigating/fixing them in Dotty (and perhaps later in Scala 2.x). These are based on Scala 2.x behavior, which I understand Dotty follows closely, apologies in advance if it has already been fixed
You can inherit from case classes and extend them with new functionality:
@ case class Foo(i: Int)
defined class Foo
@ Foo(1)
res18: Foo = Foo(1)
@ Foo(1).i
res19: Int = 1
@ class Bar extends Foo(1)
defined class Bar
@ (new Bar).i
res21: Int = 1
You can even declare it an abstract case class
to force someone to inherit
from it rather than instantiating it directly. If you want your
case class to not allow inheritance you should label it final
As far as I can tell, “nobody” does any of this: people don’t inherit from case
classes, declare their case classes abstract
, or rememebr to mark them
final
. Literally the only programmer I’ve ever seen making good use of
abstract
case classes and inheritance is probably Martin Odersky himself. I
think we should disallow it, and just force people to use normal classes if
they want to inherit from them.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:32 (17 by maintainers)
Top GitHub Comments
generally I don’t think the compiler should be responsible for warts and linting. The compiler should either allow or disallow certain features that are planned to be removed, with a flag to ignore the warning, allowing a grace time for users to migrate. Scalafix is proving itself to be an excellent tool in this regards, allowing the compiler to be simpler.
given the usecase in scalaz, I’m sure having a record of how this works for posterity would be good.
compiler development seems to be an endless amount of moving people to other services or rooms to have a conversation. If I ever write a compiler, I’ll be sure to spend a good amount of time setting up at least 5 mailing lists, gitter rooms, github accounts, discourse sites and twitter accounts… just so I can do a basic job of sending people to the right place.