I have a skippable function that is unexpectedly n...
# compose
e
I have a skippable function that is unexpectedly not being skipped depending on where a
Boolean
literal is being passed in from. Anyone have any idea why this is happening? Code in 🧵
As the
TopAppBar
is scrolled,
Scaffold's
content is recomposed (which makes sense since
innerPadding
is changing). I would expect
MyContent
to be skipped (since it takes a stable lambda and a parameter that is set to a
Boolean
literal), but it is called on every recompose. However, if I hardcode
MyContent's
isVisible = true
instead of
isVisible = isVisible
then it does get skipped.
Copy code
@Composable
fun Test() {
  MyContentWrapper(
    content = {},
    isVisible = true
  )
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MyContentWrapper(
  content: @Composable () -> Unit,
  isVisible: Boolean = true
) {
  Scaffold(
    topBar = {
      TopAppBar(
        title = { Text(text = "TopAppBar") },
        scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
      )
    }
  ) { innerPadding ->
    Box(
      modifier = Modifier.padding(innerPadding)
    ) {
      MyContent(
        content = content,
        isVisible = isVisible
      )
    }
  }
}

@Composable
private fun MyContent(
  content: @Composable () -> Unit,
  isVisible: Boolean
) {
  Log.e("TEST", "RECOMPOSE")
  if(isVisible) {
    content()
  }
}
b
I'm not 100% sure but I would assume it has something to do with the fact that Scaffold is a SubcomposeLayout underneath and that is causing the parameter you are passing in to be determined as uncertain
e
Should I file an issue about it, or is that just the way it would work with SubcomposeLayout?
b
Yeah it is working as intended
e
Out of curiosity why can't it pass along the stability information?
If I wrap the
Boolean
in an
Immutable
data class even outside the scope of
Scaffold
then it doesn't recompose.
b
I wouldn't have guessed that, my theory might be wrong
I don't know enough about SubcomposeLayout sorry, you could try file a bug if you don't get a better answer
e
Thanks, I'll wait until tomorrow to see if anyone else has thoughts, and file a bug if there are none.