4 ms·
Yeah, that's exactly what I meant. The JVM is designed to execute the constructs of the Java language. Scala compiles to JVM bytecodes, but it does a lot of (v
by pcl 6y ago
Yeah, that's exactly what I meant.
The JVM is designed to execute the constructs of the Java language. Scala compiles to JVM bytecodes, but it does a lot of (very cool!) things that are not natively supported by the JVM, and thus tend to be pretty expensive. Scala's pattern matching, for example, goes beyond what can be efficiently expressed in the JVM.
As a rule, Kotlin does not take this approach. Instead, almost all the constructs in Kotlin can be cleanly expressed in JVM bytecodes.
Reified generics is a good example. Kotlin only allows them when a function can be inlined, since in those situations, you don't actually need generic arguments, since the code in the inlinable function can be dropped in place. This allows for the convenience of reified generics -- but only if you're willing to live within the limitations of the inline restriction. (And it's also elegant, since it'll be easy to relax the constraint once the JVM adds the needed support.)
I've run across one or two things in Kotlin that don't translate to JVM bytecode elegantly, although I'm struggling to find a good example off the cuff. They're definitely the exception, though, whereas the Scala team took a more radical approach.
- The_rationalist 6y agoIt's not clear that bytecode changes are needed for efficient pattern matching support. Java is getting pattern matching support. The related JEPs mention that: The implementation will likely make use of dynamic constants https://openjdk.java.net/jeps/309 https://openjdk.java.net/jeps/309 I strongly suspect that Scala could be more efficient if it used it too. However I have no idea if this could improve compile time. (kotlin has until now procrastinated implementing pattern matching support because they say it increase compile time, but maybe that doesn't hold with constant dynamics)