What does this print?
inline fun run(f: () -> Unit) {
f()
}
fun check(n: Int): Int {
run {
if (n < 5) return 0
}
return n
}
fun main() {
println(check(3))
}
Try it in the online Kotlin Playground →
[spoiler title="Solution"]
Answer:
0
Explanation:
Inline functions enable non-local returns. The return inside the lambda returns from check(), not the lambda. Without inline, this code wouldn't compile.
[/spoiler]