TL;DR
The blog post talks about Kotlin, a programming language that’s similar to Java but with new features. The post explains the differences between anonymous functions and lambdas in Kotlin, including their syntax and return types. It also gives examples of how anonymous functions can be useful when a developer wants to return only from a specific block of code, rather than the entire function.
After spending most of my career as a Java developer, I’ve recently changed and moved to the different but similar world of Kotlin. While it is much easier for a Java developer to make a shift to Kotlin instead of Python or Go, beneath the surface, there are lots of new things to be explored. In this blog miniseries, I’ll write about topics that have left an impression on me and I found interesting in my ongoing Kotlin exploration journey.
In the last post, we saw some of the basic characteristics of lambda expressions and how they can be used. Lambdas are pretty cool, aren’t they? We have also mentioned lambdas can be considered an anonymous function. But the news is, in Kotlin there is a type of function that is called an anonymous function. And it is declared differently from Lambda expressions. And there is more difference than just the syntax. Let’s see what is all it about.
Anonymous function declaration if Kotlin looks very much like a regular method, with one exception - it does not have a name:
fun(myName: String) = println("My name is " + myName)
From this code fraction, we can already notice anonymous functions and lambdas have different way of definition. The same logic would be expressed via lambda in the following way:
val myLambda = {myName -> println("My name is " + myName)}
As one can probably assume, there are more differences between these two than syntax and that is the return type. As it can be read in the official Kotlin documentation:
a
return
inside a lambda expression will return from the enclosing function, whereas areturn
inside an anonymous function will return from the anonymous function itself.
Since our previous example did not have any return type, let’s spot the difference with some new examples:
fun testIt(): Boolean {
val items = listOf(-1, 2, 3, 4, 5)
items.forEach {
if (it < 0) return false // returns from the method testIt()
}
print("Hello from testIt()")
return true
}
The lambda functions are designed with the rule that as soon return statement is reached, it should return from the nearest function declared with the fun keyword. In our example, that means when the if condition returns true, the method would be completely exited and the statement in the print method would never be reached. And this might not always be the scenario the developer needs. This is where the anonymous functions can be leveraged:
fun testIt() : Boolean {
val items = listOf(-1, 2, 3, 4, 5)
items.forEach(fun(element) {
if (element < 0) return // returns only from block
})
print("Hello from testIt()")
return true
}
If you try to run this code segment, the difference can be immediately noticed - the return is only for the block of code that is executed and the print statement will always be executed. Having this in mind, the separation definitelly makes more sense! And depending on our code and requirements, we can always choose the one that makes our life easier!