Kotlin scope functions: let, run, with, also, apply
functions that allow for changing the scope, or the range, of a variable
Hello Everone, in this article we will discuss the kotlin scope function, so the first question arises is that what is scope function is.
In Simple words. Kotlin's “scope functions” change the scope or range of a variable. The purpose of scope functions is to make code more readable, clear, and concise.
Developers generally encounter a lot of difficulties when it comes to dealing with Scope Functions.
So in this article, we will be discussing, what are scope functions, what are types of different scope functions, what are the differences between those scope functions, Most importantly, when to use them, where to use them and how to use them in your application. And in the last, I will summarize all those in the simplest possible way which will add more sense to our understanding.
So Let’s get started, there are 5 types of Scope functions. They are,
- WITH
* Return: Lambda result (The Return value is the “Lambda result”)
* Context object: this(Refer to context object by using “this”)
* If you want to operate on a non-null object. - LET
* Return: Lambda result(The Return value is the “Lambda result”)
* Context object: this(Refer to context object by using “this”)
* If you want to just execute lambda expression on a nullable object and avoid NullPointerException. - RUN
* Return: Lambda result(The Return value is the “Lambda result”)
* Context object: this(Refer to context object by using “this”)
* If you want to operate on a nullable object, execute lambda expression and avoid NullPointerException(it’s also a combination of WITH function and LET function) - APPLY
* Return: Lambda result(The Return value is the “Lambda result”)
* Context object: this(Refer to context object by using “this”)
* If you want to initialize or configure an object. - ALSO
* Return: context object (The Return value is the “context object”)
* Context object: it (Refer to context object by using “it”)
* If you want to do some additional object configuration or operations.
let's Discuss one by one, So first is WITH function
- WITH
So for WITH function, the return type is the lambda result. and the context object is …. ‘this’ which refers to the object itself. To understand things better, let us explore a simple demo of WITH function.
its recommend for calling functions on the context object without providing the lambda result.
2. APPLY
let’s check out the APPLY function. Well, the apply function returns the context object itself. Please note this. and we refer to the context object using the ‘this’ keyword… similar to what we said earlier. So let’s see how to use this function.
When you initialize an object and set a bunch of properties, like here, you have a good candidate to APPLY the scope function.
the apply function returns the context object itself. i.e. this whole expression will return the person details object we are creating. So here on the left, we can use val person details equal to… the apply function. fine? If this statement confuses you. then you can split it into two statements. Like this. First, create the object and then apply values to it. Nice and simple. But generally, we don't use it this way in two statements. We do it a one-liner. Fine? Now, you can then use the with function to print the values. Let us run the program. Perfect. So this is how we use the APPLY function along with the WITH function.
3. ALSO
Moving ahead, let’s check out the ALSO function. Well, this function returns the context object. And you can refer to the object by using the ‘it’ identifier. So let’s see how to use this function. Well, this ‘also’ function is generally used where we want to perform some additional operation on a particular object after we initialize it.
It is used in situations where we have to perform additional operations once the object’s members have been initialized.
4. LET
Moving forward, we have the LET function. Well, let function returns the lambda result. and you can refer to the context object by using the ‘it’ identifier.
Let’s understand it better in a program. Now, the LET function has a lot of applications. But I am going to show you the most important usage of this function.
Generally, we use the LET function to avoid NULL pointer exceptions.
Which is a nightmare for most developers across the world. Let’s see how.
As per the above example, only the first code two print state is printing value, the other two are not because the age variable is null.
By applying ?.let to an object, we can be assured that each time we access the object inside the scope function, the object will not be null. To reference the object inside the scope function, we use the keyword it.
But trust me usually use the LET function, for avoiding NullPointerException. So yes that’s all for the LET function.
5. RUN
It’s time to go ahead and explore our last Scope Function… i.e. the RUN function. Well, the run function returns the lambda result and you can refer to the context object by using the ‘this’ keyword. Now, the RUN function is actually the combination of the WITH function and the LET function.
Using RUN, we can perform null safety checks as well as other computations when the object lambda contains both initialization and computation.
if you are feeling jumbled or confused about when to use what… then I don’t blame you for that. Using scope functions at the right place is a bit tricky. So let’s summarise this article and give you a big picture of what we have learned so far. You can use WITH function if you want to operate on a non-null object. You can use the LET function, if you want to just execute lambda expression on a nullable object and avoid NullPointerEception Next, use the RUN function, If you want to operate on a nullable object, execute lambda expression and avoid NullPointerEception Moving on, you can use APPLY function, If you want to initialize or configure an object And finally, you can use ALSO function If you want to do some additional object configuration or operations So in future, whenever you feel confused about when to use what…. just revisit this part of the article. Things will automatically get sorted. So I hope you had a great time learning concepts in this article.
If you got something wrong? Mention it in the comments. I would love to improve. your support means a lot to me! If you enjoy the content, I’d be grateful if you could consider subscribing to my YouTube channel as well.
I am Shirsh Shukla, a creative Developer, and a Technology lover. You can find me on LinkedIn or maybe follow me on Twitter or just walk over my portfolio for more details. And of course, you can follow me on GitHub as well.
Have a nice day!🙂