Post AXek6xRfz0ATmg0Ab2 by alexelcu@social.alexn.org
 (DIR) More posts by alexelcu@social.alexn.org
 (DIR) Post #AXeAPXOtRd5a9xGCgK by alexelcu@social.alexn.org
       2023-07-13T09:07:09Z
       
       0 likes, 0 repeats
       
       In #Scala, it would be cool to have some linter option for banning downcasting / isInstanceOf on open types.
       
 (DIR) Post #AXeT4lEihqME7QJAwa by djspiewak@fosstodon.org
       2023-07-13T12:36:17Z
       
       0 likes, 0 repeats
       
       @alexelcu would you ban pattern patching too?
       
 (DIR) Post #AXejjsPOXl4HVCvdrs by alexelcu@social.alexn.org
       2023-07-13T15:43:01Z
       
       0 likes, 0 repeats
       
       @djspiewak I'd like that, yes — with some annotation to turn it off when absolutely needed.Pattern matching in Scala does not make `instanceOf` checks safe.The problem with instanceOf & downcasting is that it's an implementation leak, violating Liskov's substitution principle. Which is manifested every time you wrap an object in another object, which is even encouraged as a best practice (composition instead of inheritance!). Common sample: exceptions wrapping other exceptions (via `cause`).
       
 (DIR) Post #AXek4e05JA84tgzcEi by tpolecat@mastodon.social
       2023-07-13T15:46:41Z
       
       0 likes, 0 repeats
       
       @alexelcu @djspiewak Stephen Compall makes a good case that pattern-matching is ok in the same cases where you could have written it as a fold, although it seems like a stretch for this compiler to be able to figure it out. https://typelevel.org/blog/2014/11/10/why_is_adt_pattern_matching_allowed.html
       
 (DIR) Post #AXek6xRfz0ATmg0Ab2 by alexelcu@social.alexn.org
       2023-07-13T15:47:12Z
       
       0 likes, 0 repeats
       
       @djspiewak Every time we do an `isInstanceOf` on an open type, we only do that because we know details of the implementation. And that's bad because implementations change, and downstream consumers of the API shouldn't break when the types don't change.`isInstanceOf` and downcasting are only acceptable as optional optimizations that don't affect the correctness of the code.
       
 (DIR) Post #AXekxEgz6X9uKh5evA by vilunov@fosstodon.org
       2023-07-13T12:56:31Z
       
       0 likes, 0 repeats
       
       On open types it would be a good idea. Types should be particularly marked to support downcasting, in Scala this is the `sealed` keyword.
       
 (DIR) Post #AXekxFYrsCGD1oIguO by djspiewak@fosstodon.org
       2023-07-13T14:05:16Z
       
       0 likes, 0 repeats
       
       @vilunov Interestingly, one of Scala's unique contributions to the field has always been the fact that it *doesn't* ban pattern matching on open types and has a reasonable way of making that work. I'm not sure I'm totally sold on just turning that off entirely.
       
 (DIR) Post #AXekxGHBDRhpFF24ye by alexelcu@social.alexn.org
       2023-07-13T15:56:37Z
       
       0 likes, 0 repeats
       
       @djspiewak On this point, I agree that Scala's flexibility is a super-power. I also like `null`, actually, in Scala 3, in the context of a runtime that makes boxing expensive.But I'd like to make some things harder, such that people don't default to it.@vilunov
       
 (DIR) Post #AXfyepTxgmdFRu4mSu by hamnis@snabelen.no
       2023-07-14T06:04:47Z
       
       0 likes, 0 repeats
       
       @alexelcu This can be written as a scalafix linting rule if you want.