Example of when should we use run, let, apply, also and with on Kotlin

All these functions are used for switching the scope of the current function / the variable. They are used to keep things that belong together in one place (mostly initializations).

Here are some examples:

run – returns anything you want and re-scopes the variable it’s used on to this

val password: Password = PasswordGenerator().run {
       seed = "someString"
       hash = {s -> someHash(s)}
       hashRepetitions = 1000

       generate()
   }

The password generator is now rescoped as this and we can therefore set seed, hash and hashRepetitions without using a variable.
generate() will return an instance of Password.

apply is similar, but it will return this:

val generator = PasswordGenerator().apply {
       seed = "someString"
       hash = {s -> someHash(s)}
       hashRepetitions = 1000
   }
val pasword = generator.generate()

That’s particularly useful as a replacement for the Builder pattern, and if you want to re-use certain configurations.

let – mostly used to avoid null checks, but can also be used as a replacement for run. The difference is, that this will still be the same as before and you access the re-scoped variable using it:

val fruitBasket = ...

apple?.let {
  println("adding a ${it.color} apple!")
  fruitBasket.add(it)
}

The code above will add the apple to the basket only if it’s not null. Also notice that it is now not optional anymore so you won’t run into a NullPointerException here (aka. you don’t need to use ?. to access its attributes)

also – use it when you want to use apply, but don’t want to shadow this

class FruitBasket {
    private var weight = 0

    fun addFrom(appleTree: AppleTree) {
        val apple = appleTree.pick().also { apple ->
            this.weight += apple.weight
            add(apple)
        }
        ...
    }
    ...
    fun add(fruit: Fruit) = ...
}

Using apply here would shadow this, so that this.weight would refer to the apple, and not to the fruit basket.


Note: I shamelessly took the examples from my blog

Leave a Comment