Android ● ● ● Kotlin 2.0

Kodebits Day 36: Lambda With Receiver

Jun 7 2026
Practice lambdas with a short kotlin challenge.

What does this print?

fun build(
  block: StringBuilder.() -> Unit
): String {
  val sb = StringBuilder()
  sb.block()
  return sb.toString()
}
fun main() {
  val s = build {
    append("K")
    append("t")
  }
  println(s)
}


Try it in the online Kotlin Playground →

[spoiler title="Solution"]

Answer:

Kt

Explanation:

Lambdas with receivers run as if they are methods on the receiver.

[/spoiler]


Further Reading