4 ms·
I'm unfamiliar with Kotlin in detail. Can you explain "Functions are first-class (you can pass functions to functions)"? Are they more powerful than Java's lamb
by kjeetgill 8y ago
I'm unfamiliar with Kotlin in detail. Can you explain "Functions are first-class (you can pass functions to functions)"? Are they more powerful than Java's lambdas?
- forfengeligfaen 8y agoIn Java 7 (which is what I was using) you could not assign a method to a variable or pass it to another method. So I think it is more powerful. https://kotlinlang.org/docs/reference/lambdas.html https://kotlinlang.org/docs/reference/lambdas.html
- cmurdock 8y agoI think the big use-case is on Android where you're stuck with Java 7 and therefore can't use lambdas.
- dlubarov 8y agoThat used to be the case, but the build tools have supported lambdas for a while now, and it works even on very old devices. On the other hand, Java 8 classes like java.util.Stream only exist on API 24+ devices. So if you want to support older classes, you can't use the standard stream library.
- cmurdock 8y agoAh interesting. Thanks for the info.
- jrs95 8y agoYes, Java lambdas are just syntactic sugar over anonymous classes with a single method IIRC. You must define a full Interface to use when defining the function signature. Kotlin functions are essentially objects that can be passed around, stored in variables, etc. You can also have functions that don’t belong to any class, no need to use static methods for that.
- krzyk 8y agoNot exactly, lambdas use invokedynamic which has different performance characteristics when compared to anonymous classes. See a talk by Brian Goetz - https://www.youtube.com/watch?v=MLksirK9nnE https://www.youtube.com/watch?v=MLksirK9nnE
- jrs95 8y agoGreat, thank you for the correction. I only knew of what it looked like from an implementation perspective, had no idea things worked differently at runtime.
- guelo 8y agoThey're slightly different. Kotlin's compiler does some tricks that Java doesn't do to inline lambdas passed to functions. But Kotlin doesn't currently use Java 8's invokedynamic on the JVM, though they're planning to in the future. Not sure about on Dalvik.