DISQUS

David R. MacIver: A curious fact about overloading in Scala

  • Reinier Zwitserloot · 1 year ago
    I'm fairly sure most JVM languages, including java, won't understand this. So, cool trick, but can't use it if you need interop with other JVM languages. Which is a bit of a shame, because the whole point of JVM interop is that it's not something you program for or think about, you just get it for free.

    I'm guessing the original intent was for overloading based on just different return types not to be legal, but then this issue came up, and now its the status quo.
  • david · 1 year ago
    Indeed. It will confuse the hell out of java.lang.reflect too.
  • Daniel · 1 year ago
    Overloading based on return type was introduced for exactly one reason: When introducing generics with erasure, they needed "bridge methods", and these would often conflict with existing "real" ones. To reduce the number of these clashes, they allowed to use overloading based on return type in the JVM, but not in in the compiler for "normal" code.
  • david · 1 year ago
    No, I don't think that's the case.

    It's and covariant return types are the only cases of it actually being used in the Java implementation, but the Java virtual machine spec second edition documents this behaviour and predates both generics and covariant return.