What Is Apply in Kotlin
In Kotlin, Apply Is an Extension Function on a Particular Type and Sets Its Scope to Object on Which Apply Is Invoked. Apply Runs on the Object Reference into...
In Kotlin, apply is an extension function on a particular type and sets its scope to object on which apply is invoked. Apply runs on the object reference into the expression and also returns the object reference on completion. … It also return an object reference on completion.
What is difference between apply and also Kotlin?
Short answer: also was introduced for semantic reasons. Long answer: Use also for additional actions that don’t alter the object, such as logging or printing debug information. The common case for apply is the object configuration.
How can the apply function in Kotlin be used to configure an object?
apply scope function is used to configure the object once initialized and returns the object itself. The context object is available inside the apply function as this . To understand apply function lets look at the implementation of apply function first.