Using the 'with' Function for Object Configuration

The 'with' function in Kotlin is a convenient way to execute multiple operations on an object without repeating its reference. This is particularly useful for configuring properties or performing several actions on a single object. It enhances code readability and reduces verbosity.

val user = User().apply {
    name = "John Doe"
    age = 30
}.also {
    println("User created: $it")
}

with(user) {
    println("User Name: $name")
    println("User Age: $age")
}