Kotlin Scope Functions

Save

Kotlin Scope functions

The Kotlin standard library contains several functions that are used for executing a block of code within the context of an object. When you call such a function on an object with a lambda expression provided, it forms a temporary scope. Here, we will check five of them:-

kotlin let:-

Kotlin let is a scoping function wherein the variables declared inside the expression cannot be used outside. let takes the object it is invoked upon as the parameter and returns the result of the lambda expression.

the key ‘it’ keyword contains a copy of the property inside let.

 

Kotlin run:-

Kotlin run is another interesting function. The following example demonstrates its use cases.

Kotlin also:- 

As the name says, also expressions do some additional processing on the object it was invoked.
Unlike let, it returns the original object instead of any new return data. Hence the return data has always the same type.

Like let, also uses it too.

Kotlin apply:-

Kotlin apply is an extension function on a type. It runs on the object reference (also known as the receiver) into the expression and returns the object reference on completion.

 Kotlin with:-

Like apply, this is used to change instance properties without the need to call dot operator over the reference every time.

You can get more information regarding the scope functions and their uses in the following link:-

https://kotlinlang.org/docs/reference/scope-functions.html

 

 

 

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

It was a successful collaboration. The Mobikul team provided the client everything they needed in a timely and high-quality manner. They were very professional, reliable, and accessible during the entire process. The client also saw no room for any improvements in the vendor's performance.

Vincenzo Carrano

Vincenzo Carrano

Administrator, Segshop

Talk to Sales

Global

Live Chat
Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home