🐦 Kotlin Beginner

What does the also scope function do in Kotlin?

Answer

The also scope function executes a block with the object as its argument (accessed as it), performs a side effect, and returns the original object — not the block's result. It is similar to apply but uses it instead of this, making it clearer when you want to read the object's properties rather than configure them. Common use: val user = createUser().also { log("Created: ${it.name}") }. The word "also" reads naturally: "do this, and also log it." It is ideal for adding debugging, logging, or validation without interrupting a method chain.