Using the 'also' Function for Side Effects

The 'also' function in Kotlin is a scope function that lets you perform additional operations on an object without altering its original state. This is particularly useful for logging, modifying properties, or chaining operations while maintaining readability. It returns the original object, allowing for fluent-style code.


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