Kotlin let Keyword Tutorial

In this section, we will learn what the let keyword is and how to use it in Kotlin.

Note: we’re assuming you’re familiar with the safe call operator in Kotlin.

What is let keyword in Kotlin?

The let statement is a way of running a block of code if the value of a variable is not null.

For example, let’s say you have a variable named emp which is of nullable type. Now you want to call this variable and run a task on it with the condition of only if the value of this variable is not null!

In such a case we can create a block using the let keyword.

Alright, let’s see the syntax of this statement and then we’ll continue our discussion from there.

Kotlin let keyword declaration syntax:

variable?.let{

//Instructions to run if the value of the variable is not null.

}

Here, the variable is the one that is of type null and might not have an actual value! So we run the safe call operator to see if there’s a value behind the variable or not.

Now, if and only if there’s an object for this variable, then the block of instructions that is defined in the body of the let keyword will be executed.

Note that we used the dot operator and the let keyword with its block {} after the name of the nullable variable.

Kotlin let and it keywords

Within the body of the let block, we can use a keyword named it which refers to the object that the related variable (the one that the let block is created for) is referring to.

So using this it keyword, we can invoke any member of that object.

Example: using the let keyword in Kotlin

class Address{
    var city:String = "LA"
    var state:String = "CA" 
}

class Employee{
    var address:Address? = null
    var firstName:String = "John"
    var lastName:String = "Doe"
}
fun main(){
    var address = Address()

    var emp:Employee? = Employee()
    emp?.address = address 

    emp?.let{
        println("The emp variable has an object")
        println(it.address?.city)
        println(it.address?.state)
    }
}

Output:

The emp variable has an object

LA

CA

How does let keyword work in Kotlin?

Here the emp variable is called using the safe call operator and also we’ve defined a block for it to be executed if this variable does not have a null value.

Now, because the emp variable has an actual object, then the block runs and we used the it keyword to access the object and invoke its members.

Example: using let with array items

fun main(){
    var names = arrayOf("Jack","Omid",null,"John","Ellen")

    for (element in names){
        element?.let{
            println("The element of the array is: $element")
        }
    }
}

Output:

The element of the array is: Jack

The element of the array is: Omid

The element of the array is: John

The element of the array is: Ellen

Here we’ve checked every element of the array using the safe call operator and then if the value of that element wasn’t null, then the instructions defined in the let block will be run for that element.

Note: the let statement we’ve been mentioning in this section comes from something called lambda expression. We’ve devoted an entire article related to the Kotlin lambda expressions. You’ll learn about it in later sections.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies