Using the 'run' Function for Contextual Execution

The 'run' function in Kotlin is a useful scope function that allows you to execute a block of code within the context of an object. This can be particularly helpful when you want to perform multiple operations on an object and avoid repeating its name. The result of the 'run' function is the value of the last expression in the block, which can be used for further chaining or computations.

data class User(var name: String, var age: Int)

fun main() {
    val user = User("Alice", 30)

    val result = user.run {
        age += 1 // Increment age
        "User $name is now $age years old." // Return a string
    }

    println(result) // Output: User Alice is now 31 years old.
}