Learn and reasonably use scoped functions in Kotlin

Tram Ho

Nowadays most android developers have moved from java to kotlin and IT companies are starting to orient trainees to learn kotlin from the very beginning. Not that programmers turn to java, java is really a vast sky to learn it can do everything from web, mobile, network, game, … but kotlin is quite optimal in development. android mobile. With tons of features kotlin gives us short code, easy to understand and less bugs.

In this article, I want to join you in understanding and delving into one of the features that kotlin gives us. That is the title of the article.

Certainly, someone who learned kotlin has heard this definition and more or less watched it. But not necessarily understood and still wondered when to use any. Then let’s find out together!

1. What is scoped function in kotlin?

There are five types of scoped functions in kotlin: let, run, with, also and apply. We will explore them thoroughly in the next section.

So what are they?

These functions give us a way to access the temporary scope of the object under consideration, where specific operations can be applied to the object in the code block.

Here I take a funny example just to make it easy for you to understand: Imagine that you use the scoped functions to enter a house, use the furniture in that house. Depending on the scoped function, what you do in it. The rest, you can imagine yourself in the next section.

To enter the next part, there are two keywords you need to understand that is it and this . Both of these keywords will be a bit confusing but don’t worry. Let’s go to examples to understand it below.

2. Learn the types of scoped through example:

We have a class like this:

let

Even though let operates in the person object, the output is the name of person. This means that let gives us access to the object and returns any value depending on our use case. Very convenient is not it. For example, we need to access a certain object and do some logic to get the required value based on properties in the object. Like the simple example below:

Note it will return the bottom unassigned command line in scope.

In the case that within the scope of the last line there is an assigned data line, the return value will be of type Unit in kotlin.

Other let benefits include:

  • It references the person object with the keyword it . And yes it is possible to replace the value of person through it . As for the case of using nested let , we can easily replace the keyword it by another keyword as shown below:

  • It makes it easy for us to check for null. If null is ignored, if not null, it will enter its scope block as example.

  • It helps us to refer to the result of a linked code as shown below:

We usually assign a calculated result to a variable and then display it again. As for let, it helps us to immediately refer to the result as follows:

There is no need to declare additional variables to get results.

You must feel quite long reading here, right? Rest assured that the following operators will be similar to let, I just pointed out the differences.

trembling

The run operator is similar to let operator, which gives access to an object and returns the desired result different from the object.

let & run

So what’s the difference?

It is run that references the object through the this operator. In the scope of the run operator, we will not need to use this to access the property, just writing the property name is enough. Also, it is not possible to rename this as the let operator.

run also gives us a null test similar to let . Therefore, depending on the case, we need to choose one of these two useful operators.

with

The with operator is quite similar to the run operator we discussed above. It also references the object with the this keyword. But the difference is that it doesn’t provide us with a method to check for nulls.

Instead of having to check null line by line 1, switch to run as below:

It will be more convenient and neat, isn’t it!

apply

The re-apply operator is similar to the run operator above. It references the object with this keyword and cannot change it by a different name.

And also gives us a way to check for null.

run & apply

The difference is that the apply operator does not allow us to return any value other than the object we are referencing.

also

The operator is also quite similar to let. It references the object through the keyword it. And gives us the ability to check null.

let & also

The difference between these two operators is that they also allow us to access the object and modify the object but not allow us to return values ​​other than the object like let .

3. Summary:

To summarize the article, there will be a few short descriptions of the knowledge you have mentioned as follows:

About how to choose each of these operators, depending on the purpose and use case, the figure below will help you find a suitable operator for your problem as follows:

Hope this article will help you to read. If you have any questions or contribute anything, please leave a comment below. Thank you !

Refer:

https://blog.mindorks.com/using-scoped-functions-in-kotlin-let-run-with-also-apply

Share the news now

Source : Viblo