Travis Griggs
01/25/2024, 1:12 AM@Composable
fun foo() {
bar(onAnswer = { v -> v + 42 })
baz(onAnswer = { v -> v + 42 })
)
Not wanting to repeat myself, I have tended to extract those cases, e.g.
@Composable
fun foo() {
val onAnswer:(Int) -> Int = { v -> v + 42 }
bar(onAnswer)
baz(onAnswer)
)
Having a long career in closure friendly languages, I'm cool with this. But Kotlin has the ability to do nested functions. Which are "new" to me. So they feel a little weird. I'm used to knowing that a fun lives at a predictable scope when I see that keyword. But I could rewrite the above as:
@Composable
fun foo() {
fun onAnswer(value:Int) = value + 42
bar(::onAnswer)
baz(::onAnswer)
)
What do others do? Is there any style/convention direction here? Are there subtle nuances to consider between a val closure and nested function?ephemient
01/25/2024, 1:24 AMreturn
is a bit different, otherwise a nested function is the same as a lambdaephemient
01/25/2024, 1:25 AM{ { ... } }
or when being able to specify the return type explicitly is usefulMichael Reynolds
01/25/2024, 2:06 AMjw
01/25/2024, 2:07 AMTravis Griggs
01/25/2024, 3:43 AM