1. Introduction

When looking through code written in Kotlin, we often come across the arrow symbol ->. This operator has several use cases in Kotlin. In all of them, it basically separates two different blocks of code.

In this tutorial, we’ll take a look at how this operator works in a specific context.

2. Expression when

First, let’s discuss the arrow operator used in the when expression.

The when expression allows us to check multiple conditions and execute different code blocks based on the matching condition. The arrow symbol separates a condition with the corresponding code block that should be executed.

Let’s take a look at the when statement in the example:

val label: Int? = 1
val returned = when (label) {
     1 -> "First label"
     2 -> "Second label"
     else -> {
         "Label is neither first nor second"
     }
}
assertEquals("First label", returned)

As we can see, the when statement is used to match the value of the label variable against different patterns. In each branch, we have an arrow that separates the condition from the code that will be executed.

3. Lambda Expression

Let’s see another usage of an arrow operator in a lambda expression. In Kotlin, a lambda expression is a way to define a small anonymous function that can be used as a value. A lambda expression consists of a parameter list, an arrow operator, and a body.

Let’s take a look at an example:

val numbers = listOf(1, 2, 3, 4)
val multiplication = { x: Int, y: Int -> x * y }
val total = numbers.reduce(multiplication)
assertEquals(24, total)

In the second line, we have defined variable multiplication that contains a lambda expression that takes two integer parameters and returns their multiplication. The arrow operator separates the parameter list from the body of the lambda expression.

4. Function Type

Another notation that uses the arrow operator is a function type. Function type in Kotlin is a way to define a type that represents a function. It consists of a list of parameters and a return type, separated by an arrow operator.

There are a few ways to declare function types. The first way is a declaration function as a variable:

val multiplication: (Int, Int) -> Int = { a, b -> a * b }

As we can see, variable multiplication is a function with two parameters and returns the value of the Int type.

As Kotlin allows passing a function as a parameter, we can pass our function to another function. First, we declare a function that has a function type parameter:

fun applyOperation(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
    return operation(x, y)
}

Then, we can pass the previously declared variable as a parameter named operation:

val appliedOperation = applyOperation(2, 5, multiplication)
assertEquals(10, appliedOperation)

The third way to use the arrow operator in function type is possible by declaring the return type of a function. Let’s declare a function that returns a function:

private fun createMultiplier(factor: Int): (Int) -> Int {
    return { input -> input * factor }
}

Next, let’s see how to invoke it:

val triple = createMultiplier(3)
assertEquals(15, triple(5))

We declared a variable that was used like any other function to multiply its input by the corresponding factor.

5. Conclusion

In this article, we’ve seen the arrow operator in practice. The main function of the arrow operator is to separate code blocks, and it’s used in many syntax constructs in Kotlin.

As usual, all the examples are available over on GitHub.