Read More

Kotlin Scope Functions

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 parametPreview (opens in a new tab)er 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

 

 

 

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close