// Tutorial //

Kotlin let, run, also, apply, with

Published on August 3, 2022
Default avatar

By Anupam Chugh

Kotlin let, run, also, apply, with

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

In this tutorial, we’ll be implementing some of the important standard library functions available in Kotlin. The kotlin-stdlib provides us with useful higher order functions implementing idiomatic patterns. We’ll see how they make programming in Kotlin so easier and faster. The functions that we’re going to discuss below are:

  • let
  • run
  • also
  • apply
  • with

Kotlin let

let takes the object it is invoked upon as the parameter and returns the result of the lambda expression. Kotlin let is a scoping function wherein the variables declared inside the expression cannot be used outside. An example demonstrating kotlin let function is given below.

fun main(args: Array<String>) {
    var str = "Hello World"
    str.let { println("$it!!") }

//Hello World!!
//Hello World

it keyword contains the copy of the property inside let. The last value from the let is returned as an argument as shown below.

var strLength = str.let { "$it function".length }
println("strLength is $strLength") //prints strLength is 25

Kotlin let example

Chaining let functions

var a = 1
var b= 2

a = a.let { it + 2 }.let { val i = it + b
println(a) //5

As you can see we’ve declared a local variable “i” inside the second let function. Setting the last statement of the let function to i returns the property to the outer property a. kotlin let chaining

Nesting let

We can set a let expression inside another let expression as shown below.

var x = "Anupam"
x.let { outer -> outer.let { inner -> print("Inner is $inner and outer is $outer") } }

//Inner is Anupam and outer is Anupam

For nested let, we can’t use it keyword. We need to assign explicit names to it in both the let functions. Only the outermost let returns the value as shown below.

var x = "Anupam"
    x = x.let { outer ->
        outer.let { inner ->
            println("Inner is $inner and outer is $outer")
            "Kotlin Tutorials Inner let"
        "Kotlin Tutorials Outer let" 
    println(x) //prints Kotlin Tutorials Outer let

kotlin nested let

let for null checks

Additionally, let is useful for checking Nullable properties as shown below.

var name : String? = "Kotlin let null check"
name?.let { println(it) } //prints Kotlin let null check
name = null
name?.let { println(it) } //nothing happens

The code inside the let expression is executed only when the property is not null. Thus let saves us from the if else null checker too!

Kotlin run

Kotlin run is another interesting function. The following example demonstrates its use cases.

var tutorial = "This is Kotlin Tutorial"
    println(tutorial) //This is Kotlin Tutorial
    tutorial = run {
        val tutorial = "This is run function"
    println(tutorial) //This is run function

Kotlin run function Kotlin run expression can change the outer property. Hence in the above code, we’ve redefined it for the local scope.

  • Similar to the let function, the run function also returns the last statement.
  • Unlike let, the run function doesn’t support the it keyword.

let and run

Let’s combine the let and run functions together.

var p : String? = null
    p?.let { println("p is $p") } ?: run { println("p was null. Setting default value to: ")
        p = "Kotlin"}

//p was null. Setting default value to: 

Kotlin also

As the name says, also expressions does some additional processing on the object it was invoked. Unlike let, it returns the original object instead of any new return data. Hence the return data has always the same type. Like let, also uses it too.

var m = 1
m = m.also { it + 1 }.also { it + 1 }
println(m) //prints 1 

Kotlin also function

Kotlin let vs also

Following code snippet shows a great example to differentiate between let and also.

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

var l = person.let { it.tutorial = "Android" }
var al = person.also { it.tutorial = "Android" }

Kotlin let vs also In the above code, we’ve used Data classes. The also expression returns the data class object whereas the let expression returns nothing (Unit) as we didn’t specify anything explicitly.

Kotlin apply

Kotlin apply is an extension function on a type. It runs on the object reference (also known as receiver) into the expression and returns the object reference on completion.

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

person.apply { this.tutorial = "Swift" }

Kotlin apply

apply vs also

data class Person(var n: String, var t : String)
var person = Person("Anupam", "Kotlin")

person.apply { t = "Swift" }

person.also { it.t = "Kotlin" }

Kotlin apply vs also Note: In apply it isn’t allowed. If the property name of the data class is unique in the function, you can omit this. We should use also only when we don’t want to shadow this.

Kotlin with

Like apply, with is used to change instance properties without the need to call dot operator over the reference every time.

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

        name = "No Name"
        tutorial = "Kotlin tutorials"

Kotlin with function Again with is similar to apply except for a few differences.

Kotlin apply vs with

  • with runs without an object(receiver) whereas apply needs one.
  • apply runs on the object reference, whereas with just passes it as an argument.
  • The last expression of with function returns a result.
var xyz = with(person)
        name = "No Name"
        tutorial = "Kotlin tutorials"
        val xyz = "End of tutorial"
    println(xyz) //End of tutorial

That’s all for Kotlin standard functions to alter variables or modify objects within the function.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors
Default avatar
Anupam Chugh


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
DigitalOcean Employee
DigitalOcean Employee badge
June 8, 2021

let null check code with give you a compile time error if anyone copies and paste it from here… !! Otherwise its very informative. Thanks.

- Soumita

    DigitalOcean Employee
    DigitalOcean Employee badge
    October 14, 2020

    I think it’s the best article I have read on kotlin scoping function. Crystal clear information.

    - Astha

      DigitalOcean Employee
      DigitalOcean Employee badge
      October 13, 2020

      Thanks, great examples and good job explaining the differences.

      - Gary

        DigitalOcean Employee
        DigitalOcean Employee badge
        May 26, 2020

        Very nice easy to understand each one

        - Abhishek Srivastava

          DigitalOcean Employee
          DigitalOcean Employee badge
          May 16, 2020

          Thanks! Very informative and understandable article.

          - Anchith

            DigitalOcean Employee
            DigitalOcean Employee badge
            February 23, 2020

            Very clear and specific

            - Alif Hasnain

              DigitalOcean Employee
              DigitalOcean Employee badge
              January 22, 2020

              give me some internal mechanism about these functions.

              - Nareshkumar

                Try DigitalOcean for free

                Click below to sign up and get $200 of credit to try our products over 60 days!

                Sign up

                card icon
                Get our biweekly newsletter

                Sign up for Infrastructure as a Newsletter.

                Sign up
                card icon
                Hollie's Hub for Good

                Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

                Learn more
                card icon
                Become a contributor

                You get paid; we donate to tech nonprofits.

                Learn more
                Welcome to the developer cloud

                DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

                Learn more ->
                DigitalOcean Cloud Control Panel
                Get started for free

                Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

                New accounts only. By submitting your email you agree to our Privacy Policy.

                © 2023 DigitalOcean, LLC.