let and run: Transform and Scope
Apply let for null-safe transformations and run for local computations.
let and run: Transform and Scope is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
let and run
let uses it as the receiver; run uses this. Both return the lambda's last expression. Pick based on whether you prefer explicit references.
let Basics
let passes the receiver to the lambda as it. Useful for transforming a value or limiting a variable's scope.
fun main() {
val length = "Kotlin".let { it.length }
println(length) // 6
}let with Nullable
Classic idiom: obj?.let { ... } runs the block only when obj is non-null.
fun main() {
val name: String? = "Ada"
val upper = name?.let { it.uppercase() } ?: "(unknown)"
println(upper) // ADA
}let to Transform
Use let to convert a value while keeping the chain readable.
fun main() {
val numStr = "42"
val doubled = numStr.let { it.toInt() }.let { it * 2 }
println(doubled) // 84
}let to Limit Scope
Restrict a variable's visibility to a small block — clean way to avoid namespace pollution.
fun main() {
val username = readUserConfig().let { config ->
config.uppercase()
}
println(username)
}
fun readUserConfig() = "ada"run Basics
run works like let but uses this instead of it. Useful when you want to call several members of the receiver.
class Rect(val w: Int, val h: Int) {
fun area() = w * h
fun perimeter() = 2 * (w + h)
}
fun main() {
val description = Rect(3, 4).run {
"area=${area()}, perimeter=${perimeter()}"
}
println(description)
}run as a Standalone Block
run { ... } (without a receiver) runs the block and returns its result — useful for grouping computations.
fun main() {
val result = run {
val a = 10
val b = 20
a + b
}
println(result) // 30
}let vs run by Receiver Style
If the lambda body accesses many members of the receiver, run avoids it.. If it uses the receiver just once or twice, let reads fine.
class User(val name: String, val email: String, val age: Int)
fun main() {
val user = User("Ada", "ada@x.io", 35)
// run: this-style
val s1 = user.run { "$name <$email> age $age" }
// let: it-style
val s2 = user.let { "${it.name} <${it.email}> age ${it.age}" }
println(s1); println(s2)
}Combining let and Elvis
The combination of ?.let { ... } and ?: is a powerful null-safe pipeline pattern.
fun main() {
val raw: String? = " hello "
val cleaned = raw?.let { it.trim().uppercase() } ?: "(empty)"
println(cleaned) // HELLO
}run with Configuration and Result
Combine setup (via this) with a returned value in one block.
class Connection {
var host: String = ""
fun connect() = "connected to $host"
}
fun main() {
val status = Connection().run {
host = "localhost"
connect()
}
println(status)
}Anti-Pattern: Overusing let
Don't wrap simple null checks in let when an if is clearer. Use scope functions where they actually help.
fun main() {
val name: String? = "Ada"
// OK
name?.let { println(it.length) }
// Equivalent and clearer for trivial cases:
if (name != null) println(name.length)
}Quick Check
Which scope function passes the receiver to the lambda as it AND returns the lambda result?
Recap
let uses it, returns lambda result — great for null-safe transformations and scope limiting. run uses this, returns lambda result — great when you call many members of the receiver. Both excel at expression-style code.
Frequently asked questions
Is the “let and run: Transform and Scope” lesson free?
Yes — the full text of “let and run: Transform and Scope” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “let and run: Transform and Scope”?
Apply let for null-safe transformations and run for local computations. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “let and run: Transform and Scope” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.