Introduction
While Functional Interfaces and Function Types are used to declare the types of a function, Function Literals create the actual implementations of these types.
In this tutorial, we will learn what Kotlin Function Literals are and how to create them.
Goals
At the end of the tutorial, you would have learned:
- How to create lambdas.
- How to create anonymous functions.
Tools Required
- A Kotlin IDE such as IntelliJ IDEA version
2022.2
(Community Edition). - The Kotlin version used in this tutorial is
1.7.10
.
Prerequisite Knowledge
- Basic Kotlin
- Function Types
Project Setup
To follow along with the tutorial, perform the steps below:
- Create a new Kotlin project using Gradle as the build system.
- For the Project JDK, use JDK 18.
Lambda Expressions
The syntax for a lambda expression includes the following rules:
- Must always be surrounded by curly braces.
- Parameters go inside the curly braces, to the left of the arrow (
->
). Type annotation is optional if no explicit function type is declared on the variable, parameter, and return type. - The body goes inside the curly braces, after the arrow (
->
). - Unless the return type of the lambda is Unit, the lambda implicitly returns the last expression or the single value.
Here is an example of a lambda expression:
val timesTwo: (Int) -> Int = { x: Int -> x * 2 }
Because the type declaration already specified the parameter type, we can skip that in the lambda’s parameter as well.
val timesThree: (Int) -> Int = { x -> x * 2 }
The type declaration can be skipped entirely when the lambda contains enough information for the compiler to infer the parameter and return type.
val timesFour = { x: Int -> x * 2}
Here is an example of lambda expression being used as function arguments.
fun onClick(listener: (Any)->Unit){}
…
onClick (
{ any -> }
)
…
And here is an example of lambda expression being used as return values
fun getLambda(): (Int)->String {
return { x -> "$x" }
}
This is an even shorter version of getLambda()
.
fun getLambda() = { x: Int -> "$x" }
Passing Trailing Lambda Syntax
When the last parameter of a function is a function, then you can use a special syntax called passing trailing lambda, which allows you to place the lambda expression outside the parentheses.
The onClick()
function used in the previous section
onClick (
{ any -> }
)
can be we rewritten using the passing trailing lambda syntax
onClick { any -> }
Since the listener was the only parameter on onClick()
, we can skip the parentheses entirely.
Anonymous Functions
Anonymous functions are similar to lambdas, but with only a few differences:
- You do not need to wrap it inside curly braces.
- It looks exactly the same as a regular function, but without a name.
- You can use the
return
keyword directly in the function body.
Below is an example of an anonymous function being used with a variable.
val timesTwo = fun(x: Int): Int {
return x * 2
}
timesTwo(1)
Here is an example of an anonymous function being used as a return value.
fun getLambda3(): (Int)->String {
return fun(x: Int): String{
return "$x"
}
}
and its shorter version
fun getLambda4() = fun(x: Int): String { return "$x" }
Finally, this is how you can pass an anonymous function as an argument to another function.
onClick(fun(any){})
Summary
We have learned Kotlin’s lambda expressions and anonymous functions in this tutorial.
When working on real code, I rarely see anonymous function being used over lambda expressions because anonymous functions are much more verbose.