IntermediateLambdas

apply Scope Function

Configure an object using apply.

kotlin
data class Person(var name: String, var age: Int)

fun main() {
    val person = Person("Juned", 25).apply {
        age = 26
    }
    println(person)
}
Output
Person(name=Juned, age=26)

Explanation

apply runs a block on the object and returns the object itself. Useful for configuring objects after creation.

Related Tutorials