Kotlin scope function mnemonics

Kotlin Development Kotlin Scope Functions Technology

Uzi Landsmann

Systemutvecklare

Kotlin’s scope functions Länk till annan webbplats.: apply, run, also and let are great, but how do we know which is which and when to use them? This article will help you by offering mnemonics to each of those functions, and help you remember which function to use in a given situation.

Apply: put a stamp on a package

Kotlin Scope apply

apply: put a stamp on a package


Apply is used when you have something that you want to make some changes to and then use it. For example, putting a stamp on a package:

fun applyExample() {
    val address = "Someone somewhere 12345"
    val pack = Package(address)
        .apply { this.stamp = Stamp(50.0) }

    MailService.sendPackage(pack)
}
  • The apply lambda uses this to refer to the receiving object (that is, the package that needs the stamp).
  • It returns the original value (the same package, now with a stamp).
  •  


Run: repackage a broken package

Kotlin scope 2

run: repackage a broken package


Run is used when you want to transform something into something else. For example, repackaging a broken package:

fun runExample() {
    val address = "Someone somewhere 12345"
    val stamp = Stamp(50.0)
    val brokenPackage = Package(address, stamp)
    val pack = brokenPackage.run {
        Package(this.address, this.stamp)
    }

    MailService.sendPackage(pack)
}
  • The run lambda uses this to refer to the receiving object (that is, the broken package).
  • It returns the result of the lambda (the newly created package)

Also: once the package is sent, inform the customer

Kotlin scope 3

also: once the package is sent, inform the customer


Also
is used for doing something aside from the main action. For example, informing the customer that the package is sent:

fun alsoExample() {
    val address = "Someone somewhere 12345"
    val stamp = Stamp(50.0)
    val pack = Package(address, stamp)

    MailService.sendPackage(pack).also {
        informCustomer(it.address)
    }
}
  • The also lambda uses it to refer to the receiving object (that is, the package that was sent).
  • It returns the original value (the package).

Let: Schrödinger’s package

Kotlin scope 4

let: Schrödinger’s package


Let is used often when you’re unsure whether something is null, and you want to do something with it in case it is not. For example, opening a package and checking if it contains anything:

 

fun letExample() {
    val content = openPackage()?.let {
        registerAddress(it.address)
        it.unpack()
    } ?: "nothing in there"

    playAroundWith(content)
}
  • The let lambda uses it to refer to the receiving object (that is, the package with the unknown content).
  • It returns the result value (the package content or empty space).

Summary

I tried summarizing it all up in one picture in case you want an overview of the functions and the examples:

 

Kotlin scope 5

Summary: Kotlin scope

If you’re interested, you can also look at the example code Länk till annan webbplats. I used in this article.

What about with, you say, why wasn’t it included? Well maybe it should have, but it behaves a bit differently and doesn’t really fall into the same pattern, so I omitted it. If you want to read about with or about the rest of the scope functions, I recommend Kotlin’s scope functions documentation Länk till annan webbplats..

 

Thank you for reading! Have fun using Kotlin’s scope functions!

Fler inspirerande inlägg du inte får missa