Using the 'also' Scope Function for Side Effects

The 'also' scope function in Kotlin is useful for performing side effects while allowing the object to be returned unchanged. This is particularly handy for logging, debugging, or chaining operations without modifying the original object. It takes the object as a receiver and allows you to perform operations on it within the block.


val numberList = mutableListOf(1, 2, 3, 4, 5)
numberList.also { 
    println("Original list: $it") 
}.add(6)

println("Updated list: $numberList")