Cheat Sheets

Kotlin Scope Functions Cheat Sheet: let, run, apply, also & with

By ArpaNeuro Team September 19, 2026
Kotlin scope functions cheat sheet

Scope functions run a block against an object. Pick by two axes: this versus it, and whether the block returns the object (apply/also) or a result (let/run/with). Overusing them nests into unreadable chains, so name a function when the chain gets clever.

This Kotlin cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.

Quick reference

  • let: Object is it. Returns lambda result. Great with ?.let for null and transforms.
  • run: Object is this. Returns result. Useful for computing from several properties.
  • with: Not an extension: with(obj) { }. Object is this. Returns result.
  • apply: Object is this. Returns the object. Builders and View setup.
  • also: Object is it. Returns the object. Logging/side effects in a chain.
  • takeIf: x.takeIf { predicate } returns x or null — pairs with ?.let.
  • takeUnless: Opposite predicate of takeIf.
  • Style: One scope function per chain when possible. Name it if the type is unclear.

Copy-paste examples

apply for setup, let for null, also for a side effect

If a reviewer cannot see what is returned, rewrite as a named function.

data class Request(var url: String = "", var retries: Int = 0)

fun buildRequest(path: String) = Request().apply {
    url = "https://api.example.com$path"
    retries = 3
}.also { req ->
    println("GET ${req.url}")
}

fun host(url: String?): String? =
    url?.takeIf { it.startsWith("https://") }?.let { u ->
        u.removePrefix("https://").substringBefore('/')
    }

Common mistakes

  • Nesting let inside apply inside run until nobody knows the return type.
  • Using apply when you needed a mapped value (let/run).
  • this shadowing in nested apply so you assigned the wrong receiver.
  • Scope-function chains as a substitute for a real domain function name.

FAQ

  1. let vs apply? let transforms and returns a new value (it). apply configures and returns the same object (this).
  1. also vs let? Both use it. also returns the original object; let returns the block result.
  1. Is BLOGPH0 outdated? No, but extension run is often clearer. with is nice when the object is already a local and you want several calls.

Related Kotlin cheat sheets

Build with this stack

When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds mobile app development and also sells ready-made source code. Request a quote and tell us the stack.

Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.

← All Articles Get a Quote →