concept apply in category kotlin

appears as: pply
Kotlin in Action

This is an excerpt from Manning's book Kotlin in Action.

This section demonstrates the with and apply functions from the Kotlin standard library. These functions are convenient, and you’ll find many uses for them even without understanding how they’re declared. Later, in section 11.2.1, you’ll see how you can declare similar functions for your own needs. The explanations in this section, however, help you become familiar with a unique feature of Kotlin’s lambdas that isn’t available with Java: the ability to call methods of a different object in the body of a lambda without any additional qualifiers. Such lambdas are called lambdas with receivers. Let’s begin by looking at the with function, which uses a lambda with a receiver.

The apply function works almost exactly the same as with; the only difference is that apply always returns the object passed to it as an argument (in other words, the receiver object). Let’s refactor the alphabet function again, this time using apply.

Listing 5.19. Using apply to build the alphabet
fun alphabet() = StringBuilder().apply { for (letter in 'A'..'Z') { append(letter) } append("\nNow I know the alphabet!") }.toString()

The apply function is declared as an extension function. Its receiver becomes the receiver of the lambda passed as an argument. The result of executing apply is StringBuilder, so you call toString to convert it to String afterward.

One of many cases where this is useful is when you’re creating an instance of an object and need to initialize some properties right away. In Java, this is usually accomplished through a separate Builder object; and in Kotlin, you can use apply on any object without any special support from the library where the object is defined.

To see how apply is used for such cases, let’s look at an example that creates an Android TextView component with some custom attributes.

Listing 5.20. Using apply to initialize a TextView
fun createViewWithCustomAttributes(context: Context) = TextView(context).apply { text = "Sample Text" textSize = 20.0 setPadding(10, 0, 0, 0) }

The apply function allows you to use the compact expression body style for the function. You create a new TextView instance and immediately pass it to apply. In the lambda passed to apply, the TextView instance becomes the receiver, so you can call methods and set properties on it. After the lambda is executed, apply returns that instance, which is already initialized; it becomes the result of the createViewWithCustomAttributes function.

The with and apply functions are basic generic examples of using lambdas with receivers. More specific functions can also use the same pattern. For example, you can simplify the alphabet function even further by using the buildString standard library function, which will take care of creating a StringBuilder and calling toString. The argument of buildString is a lambda with a receiver, and the receiver is always a StringBuilder.