Closures in Kotlin

Kotlin supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.

import kotlin.random.Random

// This function intSeq returns another function, which
// we define anonymously in the body of intSeq. The
// returned function closes over the variable i to
// form a closure.
fun intSeq(): () -> Int {
    var i = 0
    return {
        i++
        i
    }
}

fun main() {
    // We call intSeq, assigning the result (a function)
    // to nextInt. This function value captures its
    // own i value, which will be updated each time
    // we call nextInt.
    val nextInt = intSeq()

    // See the effect of the closure by calling nextInt
    // a few times.
    println(nextInt())
    println(nextInt())
    println(nextInt())

    // To confirm that the state is unique to that
    // particular function, create and test a new one.
    val newInts = intSeq()
    println(newInts())
}

When you run this program, you’ll see:

1
2
3
1

In this Kotlin example, we’ve created a closure using an anonymous function. The intSeq function returns another function that increments and returns a counter. Each time we call intSeq(), it creates a new instance of the anonymous function with its own i variable.

The nextInt variable holds a reference to the anonymous function returned by intSeq(). Each time we call nextInt(), it increments its own i and returns the new value.

We create a second closure newInts to demonstrate that each closure maintains its own independent state.

Closures are a powerful feature in Kotlin, allowing you to create functions with persistent, private state. They’re commonly used in functional programming patterns and can be very useful for creating iterators, callbacks, and other stateful functions.

The next feature of functions we’ll look at is recursion.