Android ● ○ ○ Kotlin 2.0

Kodebits Day 67: Scope Function Apply

Jul 30 2026
Practice scope functions with a short kotlin challenge.

What is the output?

data class Box(var w: Int = 0) {
  var h: Int = 0
}
fun main() {
  val b = Box(2).apply {
    w *= 3
    h = w + 1
  }
  println("${b.w} ${b.h}")
}


Try it in the online Kotlin Playground →

[spoiler title="Solution"]

Answer:

6 7

Explanation:

apply configures an object using this as the receiver, then returns the object. Inside apply, properties are accessed directly without qualifiers.

[/spoiler]


Further Reading