What Are the Kotlin Scope Functions?

Kaushik
The Startup
Published in
3 min readJul 21, 2020
via Author.

Scope means boundaries.

The scope defines the accessibility of any variables. In today's discussion, we will learn what are the different scope functions in Kotlin help us to build concise code for our android app.

Generally, variables declared inside the lambda expression are impossible to access from outside. During such lambda expression coding, sometime we developer would like to apply some kind of operation on it, such as maybe we would like to iterate it to apply some changes to each item? There are a total of five different types of scope functions we are going to learn in today's discussion.

Let() start with it.

The major issue we android developer faces is handling null safely while working with any null type data. The let() function is popular for handling nullable types of data. Let's check below example:

val name= Darksider

name?.let{println(it.length)}

We can call let() function on any type of object, in the above example as we can see we are calling a safe call using ?., in this scenario, let() is only called when the value of name is not null or empty or else the function will skip printing the length of the name. The let() is useful in the case where we want to perform many operations on the variable. When a variable is not null simply proceed or else just skip that operation.

apply() on it.

Who else is tired of repeating the reference object name in chaining operation? Kotlin got a solution for us all. Let’s see below example:

fun getSportCar(): Car {

return Car().apply{

carColor =”RED”

carType=”SportCar”

carDoor=”Two”

carCylinder=”Four”

}

}

we use apply() when we want an object but we want it after performing some operation on it like setting some values to the object. The returned object will be the configuration applied to it.

run() the code.

Most other scope function kotlin does have is a mix of apply() and let function. Let’s look at below example:

fun getSportCar(): Car {

return Car().run{

(carColor =”RED”

carType=”SportCar”

carDoor=”Two”

carCylinder=”Four”).toString()

}

}

we use run() whenever we want to perform some operation on the object but the result is not the same object itself, but the return type of that operation is something else.

play with() it.

with() is twins like run(). both used to make object modification and return results into different data types. The only difference stand between both is:

run() you call on some object,

while with(), you pass object inside the function. Let’s take a look at below example:

fun getSportCarString(){

val car = with(Car()){

(carColor =”RED”

carType=”SportCar”

carDoor=”Two”

carCylinder=”Four”).toString()

}

println(car)

}

There is also() one more.

also() is little bit mix of let() and apply(). Like in let() we return the object the same in also() we return the same object you call in. Look at the below example:

val name = mutableListOf(“John”, “Jhonny”)

name.also{println(“Total name in the list: $it.size”)}.add(“Jhonsan”)

so here are the total Scope functions in kotlin. Stay tuned for the next discussion on kotlin.

--

--

Kaushik
The Startup

Enthusiastic Android developer, more interested in learning anything in android mobile, car, and watch.