Using the 'run' Scope Function for Contextual Execution

The 'run' function in Kotlin is a scope function that allows you to execute a block of code within the context of an object. It is useful for performing operations on an object and returning a result without the need for an explicit reference to the object. This can lead to cleaner and more concise code, especially when initializing or configuring objects.

val user = User().run {
    name = "Alice"
    age = 30
    address = "123 Main St"
    this // returns the User object
}
println(user) // User(name=Alice, age=30, address=123 Main St)