Skip to content

Instantly share code, notes, and snippets.

@southerton81
Last active February 6, 2023 11:36
Show Gist options
  • Save southerton81/6778c08a5832ad5733295e574865e331 to your computer and use it in GitHub Desktop.
Save southerton81/6778c08a5832ad5733295e574865e331 to your computer and use it in GitHub Desktop.
Kotlin: inlined anonymous vs inlined lambda
fun f1() {
// Anonymous function: this will print 1 2, return acts like a break.
listOf(1,2).forEach(fun(it: Int): Unit {
println(it)
return
})
}
fun f2() {
// Lambda function: this will only print 1, return actually exits
// from the outer function f2(). As forEach() is an inline function, the lambda,
// that is passed to it as an argument becomes inlined.
listOf(1,2).forEach {
println(it)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment