dimsuz
06/05/2023, 4:58 PM@Composable
fun Toolbar(content: @Composable () -> Unit) { ... }
fun slotBuilder(text: String): @Composable () -> Unit = { Text(text) }
@Composable
fun Screen() {
Toolbar { Text("hello") } // A
Toolbar(content = slotBuilder("hello")) // B
}
Is A
and B
equivalent allocation-wise? In each case a new lambda will be created on each recomposition or am I wrong here?Ankit Kumar
06/05/2023, 11:03 PMLoney Chou
06/06/2023, 12:58 AMdimsuz
06/06/2023, 10:27 AMAnkit Kumar
06/06/2023, 4:13 PMremember {
{}
}
Ankit Kumar
06/06/2023, 4:14 PMdimsuz
06/06/2023, 5:51 PMremember
, because it's a composable functiondimsuz
06/06/2023, 5:52 PMB
has new lambda instance on each recomposition, while variant A
has the same lambda instance on each recomposition. I'm not seeing clearly why this is so.Loney Chou
06/06/2023, 11:25 PMLambdas will be remembered implicitly iirc.
I was talking about A case. For B case, I don't know either 😕 Maybe because it's as return type.