jeff
12/14/2021, 10:43 PMclass Helper {
val string = "string"
}
@Composable
fun RecompRepro(mutableState: MutableState<String>, helper: Helper) {
LogCompositions(msg = "Root")
Column(Modifier.height(100.dp)) {
Text(mutableState.value)
CustomComposableText(helper = helper)
}
}
@Composable
fun CustomComposableText(helper: Helper) {
LogCompositions(msg = "CustomComposableText")
Text(helper.string)
}
if I change the value of mutableState 3 times, "Root" is printed 3 times and "CustomComposableText" is printed 1 time. That is as expected.
But if I change val string
to var string
in Helper
, then all of a sudden CustomComposableText is printed 3 times. Why?jeff
12/14/2021, 10:47 PMvar
to Helper
-- it feels like it's automagically determining whether my Helper class is immutable or something...?jeff
12/14/2021, 10:47 PMIan Lake
12/15/2021, 12:14 AM@Stable
annotation can be used to indicate that a type truly is stable): https://developer.android.com/jetpack/compose/lifecycle#skippingjeff
12/15/2021, 2:09 AMCompose considers a type stable only if it can prove it.It seems to be able to "prove" that my class is stable if it only has
val
s, even ones like
val int get() = Random.nextInt()
which seems decidedly not-stable.jeff
12/15/2021, 2:15 AMIan Lake
12/15/2021, 4:07 AMget()
is inferred as stable (as it shouldn't)Ian Lake
12/15/2021, 4:07 AMjeff
12/15/2021, 3:56 PMjeff
12/15/2021, 3:56 PM