Higher-order functions and lambdas
Kotlin functions are first-class, which means they can be stored in variables and data structures, and can be passed as arguments to and returned from other higher-order functions. You can perform any operations on functions that are possible for other non-function values.
To facilitate this, Kotlin, as a statically typed programming language, uses a family of function types to represent functions, and provides a set of specialized language constructs, such as lambda expressions.
Higher-order functions
A higher-order function is a function that takes functions as parameters, or returns a function.
A good example of a higher-order function is the functional programming idiom fold
for collections. It takes an initial accumulator value and a combining function and builds its return value by consecutively combining the current accumulator value with each collection element, replacing the accumulator value each time:
In the code above, the combine
parameter has the function type (R, T) -> R
, so it accepts a function that takes two arguments of types R
and T
and returns a value of type R
. It is invoked inside the for
loop, and the return value is then assigned to accumulator
.
To call fold
, you need to pass an instance of the function type to it as an argument, and lambda expressions (described in more detail below) are widely used for this purpose at higher-order function call sites:
Function types
Kotlin uses function types, such as (Int) -> String
, for declarations that deal with functions: val onClick: () -> Unit = ...
.
These types have a special notation that corresponds to the signatures of the functions - their parameters and return values:
All function types have a parenthesized list of parameter types and a return type:
(A, B) -> C
denotes a type that represents functions that take two arguments of typesA
andB
and return a value of typeC
. The list of parameter types may be empty, as in() -> A
. TheUnit
return type cannot be omitted.Function types can optionally have an additional receiver type, which is specified before the dot in the notation: the type
A.(B) -> C
represents functions that can be called on a receiver objectA
with a parameterB
and return a valueC
. Function literals with receiver are often used along with these types.Suspending functions belong to a special kind of function type that have a suspend modifier in their notation, such as
suspend () -> Unit
orsuspend A.(B) -> C
.
The function type notation can optionally include names for the function parameters: (x: Int, y: Int) -> Point
. These names can be used for documenting the meaning of the parameters.
To specify that a function type is nullable, use parentheses as follows: ((Int, Int) -> Int)?
.
Function types can also be combined using parentheses: (Int) -> ((Int) -> Unit)
.
You can also give a function type an alternative name by using a type alias:
Instantiating a function type
There are several ways to obtain an instance of a function type:
Use a code block within a function literal, in one of the following forms:
a lambda expression:
{ a, b -> a + b }
,an anonymous function:
fun(s: String): Int { return s.toIntOrNull() ?: 0 }
Function literals with receiver can be used as values of function types with receiver.
Use a callable reference to an existing declaration:
a top-level, local, member, or extension function:
::isOdd
,String::toInt
,a top-level, member, or extension property:
List<Int>::size
,a constructor:
::Regex
These include bound callable references that point to a member of a particular instance:
foo::toString
.Use instances of a custom class that implements a function type as an interface:
The compiler can infer the function types for variables if there is enough information:
Non-literal values of function types with and without a receiver are interchangeable, so the receiver can stand in for the first parameter, and vice versa. For instance, a value of type (A, B) -> C
can be passed or assigned where a value of type A.(B) -> C
is expected, and the other way around:
Invoking a function type instance
A value of a function type can be invoked by using its invoke(...)
operator: f.invoke(x)
or just f(x)
.
If the value has a receiver type, the receiver object should be passed as the first argument. Another way to invoke a value of a function type with receiver is to prepend it with the receiver object, as if the value were an extension function: 1.foo(2)
.
Example:
Inline functions
Sometimes it is beneficial to use inline functions, which provide flexible control flow, for higher-order functions.
Lambda expressions and anonymous functions
Lambda expressions and anonymous functions are function literals. Function literals are functions that are not declared but are passed immediately as an expression. Consider the following example:
The function max
is a higher-order function, as it takes a function value as its second argument. This second argument is an expression that is itself a function, called a function literal, which is equivalent to the following named function:
Lambda expression syntax
The full syntactic form of lambda expressions is as follows:
A lambda expression is always surrounded by curly braces.
Parameter declarations in the full syntactic form go inside curly braces and have optional type annotations.
The body goes after the
->
.If the inferred return type of the lambda is not
Unit
, the last (or possibly single) expression inside the lambda body is treated as the return value.
If you leave all the optional annotations out, what's left looks like this:
Passing trailing lambdas
According to Kotlin convention, if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses:
Such syntax is also known as trailing lambda.
If the lambda is the only argument in that call, the parentheses can be omitted entirely:
it: implicit name of a single parameter
It's very common for a lambda expression to have only one parameter.
If the compiler can parse the signature without any parameters, the parameter does not need to be declared and ->
can be omitted. The parameter will be implicitly declared under the name it
:
Returning a value from a lambda expression
You can explicitly return a value from the lambda using the qualified return syntax. Otherwise, the value of the last expression is implicitly returned.
Therefore, the two following snippets are equivalent:
This convention, along with passing a lambda expression outside of parentheses, allows for LINQ-style code:
Underscore for unused variables
If the lambda parameter is unused, you can place an underscore instead of its name:
Destructuring in lambdas
Destructuring in lambdas is described as a part of destructuring declarations.
Anonymous functions
The lambda expression syntax above is missing one thing – the ability to specify the function's return type. In most cases, this is unnecessary because the return type can be inferred automatically. However, if you do need to specify it explicitly, you can use an alternative syntax: an anonymous function.
An anonymous function looks very much like a regular function declaration, except its name is omitted. Its body can be either an expression (as shown above) or a block:
The parameters and the return type are specified in the same way as for regular functions, except the parameter types can be omitted if they can be inferred from the context:
The return type inference for anonymous functions works just like for normal functions: the return type is inferred automatically for anonymous functions with an expression body, but it has to be specified explicitly (or is assumed to be Unit
) for anonymous functions with a block body.
Another difference between lambda expressions and anonymous functions is the behavior of non-local returns. A return
statement without a label always returns from the function declared with the fun
keyword. This means that a return
inside a lambda expression will return from the enclosing function, whereas a return
inside an anonymous function will return from the anonymous function itself.
Closures
A lambda expression or anonymous function (as well as a local function and an object expression) can access its closure, which includes the variables declared in the outer scope. The variables captured in the closure can be modified in the lambda:
Function literals with receiver
Function types with receiver, such as A.(B) -> C
, can be instantiated with a special form of function literals – function literals with receiver.
As mentioned above, Kotlin provides the ability to call an instance of a function type with receiver while providing the receiver object.
Inside the body of the function literal, the receiver object passed to a call becomes an implicit this
, so that you can access the members of that receiver object without any additional qualifiers, or access the receiver object using a this
expression.
This behavior is similar to that of extension functions, which also allow you to access the members of the receiver object inside the function body.
Here is an example of a function literal with receiver along with its type, where plus
is called on the receiver object:
The anonymous function syntax allows you to specify the receiver type of a function literal directly. This can be useful if you need to declare a variable of a function type with receiver, and then to use it later.
Lambda expressions can be used as function literals with receiver when the receiver type can be inferred from the context. One of the most important examples of their usage is type-safe builders: