Chain small scope functions while keeping each step explicit.

scope-chain Scope functions can be chained, but each step should still have a clear result.

Scope Chain

word
ScopeChain.kt
Replay: real traced execution (multi-file project)
fun main() {
    val word = "stone"
    val result = word.let { value ->
        value.toUpperCase()
    }.let { upper ->
        "$upper:${upper.length}"
    }

    println("word=$word")
    println("result=$result")
}
fun main() {
    val word = "go"
    val result = word.let { value ->
        value.toUpperCase()
    }.let { upper ->
        "$upper:${upper.length}"
    }

    println("word=$word")
    println("result=$result")
}
fun main() {
    val word = "kotlin"
    val result = word.let { value ->
        value.toUpperCase()
    }.let { upper ->
        "$upper:${upper.length}"
    }

    println("word=$word")
    println("result=$result")
}
  1. word ← stone, result ← STONE:5

    1fun main() {2    val word→ stone = "stone" //@word="go", "kotlin"3    val result→ STONE:5 = wordstone.let { value ->4        value.toUpperCase()5    }.let { upper ->6        "$upper:${upper.length}"7    }89    println("word=$wordstone")10    println("result=$resultSTONE:5")11}
    outputword=stone
    result=STONE:5
  1. word ← go, result ← GO:2

    1fun main() {2    val word→ go = "go"3    val result→ GO:2 = wordgo.let { value ->4        value.toUpperCase()5    }.let { upper ->6        "$upper:${upper.length}"7    }89    println("word=$wordgo")10    println("result=$resultGO:2")11}
    outputword=go
    result=GO:2
  1. word ← kotlin, result ← KOTLIN:6

    1fun main() {2    val word→ kotlin = "kotlin"3    val result→ KOTLIN:6 = wordkotlin.let { value ->4        value.toUpperCase()5    }.let { upper ->6        "$upper:${upper.length}"7    }89    println("word=$wordkotlin")10    println("result=$resultKOTLIN:6")11}
    outputword=kotlin
    result=KOTLIN:6