Using the 'also' Scope Function for Side Effects

The 'also' scope function is a great way to perform side effects on an object while returning the object itself. This can be particularly useful when you want to perform operations, such as logging or modifying properties, without changing the original object. It enhances code readability by allowing you to chain operations in a fluent style.


val user = User("John", "Doe").also {
    println("User created: ${it.firstName} ${it.lastName}")
    it.isActive = true
}