![]() |
VOOZH | about |
There are several functions in the Kotlin standard library that help in the execution of a block of code within the context of an object. Calling these functions on an object with lambda expression creates a temporary scope. These functions are called Scope Functions. We can access the object of these functions without its name. Sounds confusing! Let's see an example,
Example: Without using scope function
Output:
GeeksforGeeks
Example: Using scope function
Output:
GeeksforGeeks
You must have noticed that when we are not using the scope function, we need to write the object name every time to refer to members of the class. While using the scope function, we can directly refer to members without the object name. This is one of the ways of using the scope function. We will learn more about them in this article.
Every scope function has well-defined use cases, although all of them have nearly the same outcome. Now let’s look at each scope functions and its use cases:
Scope functions make code more clear, readable, and concise which are Kotlin language’s main features.
There are five types of scope functions:
Each of these functions is quite similar in nature with minor differences. It's often confusing to decide which function to use and when. So, we need to know what are the differences between these functions and their use cases.
Differences in these functions:
There are mainly two differences among these functions:
Note: Context object refers to the object on which we are using the scope functions. As in our previous example - ‘gfg’ is our context object
Scope functions table:
Function | Object Reference | Return Value |
|---|---|---|
let | it | Lambda result |
run | this | Lambda result |
with | this | Lambda result |
apply | this | Context object |
also | it | Context object |
Context object : it Return value : lambda result
Use Case:
let function is often used to provide null safety calls. Use safe call operator(?.) with ‘let’ for null safety. It executes the block only with the non-null value.
Example:
Output:
2
Explanation:
As you see when the value of ‘a’ is ‘null’ let function simply avoid the code block. Hence, solving the biggest nightmare of programmers - NullPointerException.
Context object : this Return value : context object
Use Case:
As the name implies - “Apply these to the object”. It can be used to operate on members of the receiver object mostly to initialize members.
Example:
Context object : this Return value : lambda result
Use Case:
Recommended use of ‘with’ for calling functions on context objects without providing the lambda result.
Example:
Output:
GeeksforGeeks
Context object : this Return value : lambda result
‘run’ function can be said as the combination of ‘let’ and ‘with’ functions.
Use Case:
Used when the object lambda contains both initialization and the computation of the return value. Using run we can perform null safety calls as well as other computations.
Example:
Output:
Company Name : Company Name : GeeksforGeeks
Explanation:
When the 'company' value is null, the body of the run is simply ignored. When it is non-null, the body executes.
Context object : it Return value : context object
Use Case:
It is used where we have to perform additional operations when we have initialized the object members.
Example:
Output:
[1, 3, 4]
There are two ways of object referencing in scope functions:
1. this
We can refer to the context object by a lambda receiver keyword - this. this keyword does object reference in ‘run’, ‘with’, and ‘apply’ functions.
Example:
Note: We can exclude this keyword to refer to members of the class.
2. it
‘let’ and ‘also’ functions refer to the object's context as a lambda argument.
Example:
There are two types of return values that a scope functions can return:
1. Lambda result
If we write any expression at the end of the code block, it becomes the return value for the scope function. Return value for ‘let’, ‘run’, and ‘with’ functions is the lambda result.
Example:
Output:
GfG's Founder : Sandeep Jain
2. Context object
‘apply’ and ‘also’ functions return the context object itself. In this case, we don’t need to specify the return value. The context object is automatically returned.
Example:
Output:
GfG's Founder : Sandeep Jain