I am trying to find out what are the differences between Kotlins and Javas lambdas. The website of Kotlin says "Kotlin has proper function types, as opposed to Java's SAM-conversions"
And I think this is the biggest difference. So you need a Functional interface in Java. And in kotlin you have function types. You dont need an extra interface.
And you can invoke lambdas in kotlin. So if you pass a lambda in java to a function, inside the body of this function you have to call the Single abstract method.
int method(IntBinaryOperator op){
return op.applyAsInt(5, 10);
}
and the kotlin way:
fun method(value: Int, op: (Int) -> Int){
print(op(value)) // You have no SAM; you can call op.invoke() or op()
}
fun main() {
method(5) {
it * it
}
}
So i think thats a hugh advantage of kotlin that you can create your own lambdas with custom number of parameter etc and that you dont need to create an extra functional interface or currying.
Another difference and advantage is the possibility to call lambdas with receivers. I think everyone know this concept so I can skip it here. You can build DSLs, you have scope functions.
Ah and you can pass lambdas outside the () if it is the last parameter. I think it is also an important difference between java and lambda. Its more readable.
I think that was the difference or did I miss something fundamental?
Update:
Thanks to @Animesh Sahu I forgot the possibility to inline lambdas
And it is possible to return the last expression inside a block. So you dont have to explicitly call 'return'