question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scala Wart: Abstract/non-final case classes

See original GitHub issue

Opening 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:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:32 (17 by maintainers)

github_iconTop GitHub Comments

4reactions
fommilcommented, Mar 30, 2018

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.

2reactions
fommilcommented, Mar 29, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warts of the Scala Programming Language
Abstract/non-final case classes ; Classes cannot have only implicit parameter lists; Presence of comments affects program logic; Conflating total ...
Read more >
Case Classes | Tour of Scala
Case classes are good for modeling immutable data. In the next step of the tour, we'll see how they are useful in pattern...
Read more >
Warts of the Scala Programming Language - Reddit
There's actually a good use case for abstract case classes. Though it's true that it would be cleaner if bare case classes would...
Read more >
Scala Best Practices - Mark case classes as final
Extending a case class will produce behaviours that, while perfectly correct, can be surprising and certainly break reasonable expectations. For example:.
Read more >
Should I use the final modifier when declaring case classes?
Why does wartremover suggest that case classes should be final? Well, because extending them isn't really a very good idea. Consider this: scala...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found