While looking for something else, quite out of mere coincidence I stumbled upon few comments about how diabolical case class inheritance is. There was this thing called ProductN
, wretches and kings, elves and wizards and how some kind of a very desirable property is lost with case classes inheritance. So what is so wrong with case class inheritance ?
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- Diffrence between assigning simple prototype and u
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- LINQ .Include() properties from sub-types in TPH i
- GRPC: make high-throughput client in Java/Scala
- Why do base Windows Forms form class with generic
- Setting up multiple test folders in a SBT project
That is not overall true. And this is worse than lie.
As mentioned by aepurniet in any case class successor which constricts a definition area must redefine the equality because pattern matching must work exactly as equality (if try to match
Point
asColoredPoint
then it will not matched sincecolor
is not exists).That give understanding to how the equality of case class hierarchy could be implemented.
Eventually it is possible to satisfy requirements of the equality relation even for case class successor (without overriding of equality).
One word: equality
case
classes come with a supplied implementation ofequals
andhashCode
. The equivalence relation, known asequals
works like this (i.e. must have the following properties):x
;x equals x
istrue
(reflexive)x
,y
,z
; ifx equals y
andy equals z
thenx equals z
(transitive)x
,y
; ifx equals y
theny equals x
(symmetric)As soon as you allow for equality within an inheritance hierarchy you can break 2 and 3. this is trivially demonstrated by the following example:
Then we have:
But not
You might argue that all class hierarchies may have this problem, and this is true. But case classes exist specifically to simplify equality from a developer's perspective (among other reasons), so having them behave non-intuitively would be the definition of an own goal!
There were other reasons as well; notably the fact that
copy
did not work as expected and interaction with the pattern matcher.