Using the 'run' Function for Scoped Configuration

The 'run' function in Kotlin is a powerful extension function that allows you to execute a block of code within the context of an object. This is particularly useful for object configuration and initialization, making your code more concise and readable. It also returns the result of the last expression within the block, which can be handy for chaining calls.

val person = Person().apply {
    name = "John Doe"
    age = 30
}.run {
    println("Created a person: $name, Age: $age")
    this // Returns the person object
}