Post AVuOAOSB1Iq89uBHNY by vrdhn@fosstodon.org
(DIR) More posts by vrdhn@fosstodon.org
(DIR) Post #AVuK22eLnmdoP1dZiK by alexelcu@social.alexn.org
2023-05-22T08:26:06Z
0 likes, 0 repeats
OOP inheritance / subtyping is great, but only works when you don't violate “Liskov's substitution principle”. The most common way that happens is via `instanceOf` checks, AKA downcasting:https://en.wikipedia.org/wiki/DowncastingDowncasting is an “implementation leak”, and “composition”, ironically, is precisely what breaks code that makes use of `instanceOf` checks.#Java #Scala #Kotlin #OOP
(DIR) Post #AVuOAOSB1Iq89uBHNY by vrdhn@fosstodon.org
2023-05-22T09:12:25Z
0 likes, 0 repeats
@alexelcu If a derived class adds public methods, downcasting is inevitable . The pattern switch and sealed classes together do a good job of handling this with some sort of compile time checking.
(DIR) Post #AVuOxRyai8g3H3jbbE by alexelcu@social.alexn.org
2023-05-22T09:21:16Z
0 likes, 0 repeats
@vrdhn A “sealed” class models a tagged union type, and doesn't classify as subtyping / OOP polymorphism, even if programming languages reuse OOP constructs for modeling them.https://en.wikipedia.org/wiki/Tagged_unionDowncasting isn't inevitable, doing it is always a choice and an implementation leak, and there are no compile-time constructs that can help because it's always a runtime check on an open class (non-final, non-sealed).
(DIR) Post #AVuQkSfR1w71YqhzqC by vrdhn@fosstodon.org
2023-05-22T09:41:20Z
0 likes, 0 repeats
@alexelcu IMO, tagged union and sealed classes are pretty much dual like objects and closures .tagged union will also require a `which exact case ` lookup at run time, which is not much different than a instenceof ..both are going to be a just a int/long comparision.
(DIR) Post #AVulMTJV4enzeWqp0K by tpolecat@mastodon.social
2023-05-22T13:32:19Z
0 likes, 0 repeats
@alexelcu For me the most demoralizing problem with subclassing is that it’s very very hard to define equality without violating Lizkov substitution, and the bugs that arise from this tend to be very subtle (putting something in a hash table and having it disappear is an old favorite).
(DIR) Post #AVulagiS2bErH2Igi0 by alexelcu@social.alexn.org
2023-05-22T13:34:54Z
0 likes, 0 repeats
@tpolecat Indeed. There's even a good article on properly defining equality in Java, with Martin Odersky's name on it 🙂https://www.artima.com/articles/how-to-write-an-equality-method-in-java