A great read for folks wondering why Java uses type erasure instead of “reified” generics. For the unaware, that means that a List<Integer> is a List<Object> at runtime. I had always wondered about it and why they didn’t take the alternative route. For context, C# does have reified types so the actual type parameter is available at runtime.

I personally love reading in depth discussions about counter intuitive engineering decisions like this.

  • Knusper@feddit.de
    link
    fedilink
    arrow-up
    2
    ·
    2 years ago

    Scala does have a way of specifying co-/contravariance, but Java actually does have that, too. In Java, you write <? extends T> for covariance and <? super T> for contravariance. Well, and generally, Scala’s generics are compatible with Java’s. It’s quite common to use Java libraries in Scala.

    One example where type erasure causes problems (which should be the same in Java):
    Imagine, you’re managing sensors and all of those sensors have a generic to remember what element will be produced by them: Sensor<Integer>, Sensor<String> etc.
    If you want to keep all of those Sensors in one big list/map, you end up with a Sensor<Object> when you take them back out.

    To retain type information, you have to put them into individual lists or somehow place a runtime type next to each element in the list, from which you can determine what to cast to.

    I guess, in languages without type erasure, it doesn’t let you place a Sensor<Integer> into the same list as a Sensor<String> to begin with, as those are strictly different types.
    But of course, those languages may offer ways of doing type erasure anyways, and obviously, you can fall back to a common interface/supertype, if the types have that.

    • JackbyDevOPM
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      2 years ago

      As a heads up, idk if it is just the Jerboa app or Lemmy but I your type params on Sensor were being interpreted as HTML tags and not rendered. The ones in the first paragraph are fine though.

      Can we use <b>raw HTML?</b>

      Edit: looks like it just gets blocked and not read.

      Edit 2: Looks like Jerboa and the browser display this differently actually lol

      • Knusper@feddit.de
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        Funnily enough, I typed that one in the Jerboa app, but yeah, it’s not rendering for me either. 🙃

        • JackbyDevOPM
          link
          fedilink
          arrow-up
          2
          ·
          2 years ago

          I opened a GitHub issue. I think it’s probably just a config thing as they are both based on the CommonMark spec. Or maybe a nug with one of the markdown libs themselves.

    • kaba0
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Note that Java’s generics are on the library-side, while Scala’s is defined at use site.

    • kaba0
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Note that Java’s generics are on the library-side, while Scala’s is defined at use site.