TL;DR
This article discusses Kotlin’s implementation of Lambda functions, which are similar to those found in Java but with some differences. Lambda functions can be declared in a variable or passed as an argument to other functions. In Kotlin, the fun keyword is not needed when declaring a lambda function, and the method is based between curly braces. The parameter’s type can be omitted, and when more arguments are needed, they are just separated by commas. Kotlin supports higher-order functions that accept lambda as a parameter. Lambda functions are anonymous functions and not the only type of anonymous function that Kotlin supports.
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 this article, our main character would be Lambdas.
Lambdas are definitely not a Kotlin specific feature. Anyone who has worked with Java 8 or higher Java versions is familiar with Lambda expressions. However, there are few differences and tweaks in how they are declared in Kotlin.
Having in mind functions are first-class citizens, lambdas are also not an exception. They can also be declared in a variable or passed as an argument to other “higher-level” functions. However, a few things should be kept in mind while declaring a lambda function. When declaring one, the fun
keyword is not needed and the method is based between curly braces:
val myLambda = {myName -> println("My name is " + myName)}
message("John")
The “arrow” -> separates the parameter(s) from the lambda’s body. As can be noticed in the code, the parameter’s type can be completely omitted. As it is already inferred by the compiler, we don’t need to declare it’s a String. For any Java people out there, I bet you noticed one more difference compared to Java code - yes parameters are not wrapped in braces! When more arguments are needed, they are just separated with a comma:
val myLambda = {name1, name2 -> println("Hello $name1 and $name2" )}
Kotlin supports higher-order functions that accept lambda (or an anonymous function) as a parameter. One example of a high-order function is forEach, which can be called on a Collection:
val items = listOf(1, 2, 3, 4, 5)
items.forEach { el -> println(el) }
When the lambda expression has one argument, it can be simplified by by replacing the lambda expression argument with the default argument name it. So the method above can look even more simple:
val items = listOf(1, 2, 3, 4, 5)
items.forEach { println(it) }
As it can be noticed in all our examples, we did not name any of our functions, we either passed them as a method argument or assigned them to a variable. That’s why lambdas are as well anonymous functions. However, they are not the only type of anonymous function Kotlin supports. But, more about that - in our next post 😉.