https://kotlinlang.org logo
#compose
Title
# compose
e

eygraber

03/14/2023, 2:33 AM
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

Ben Trengrove [G]

03/14/2023, 2:45 AM
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

eygraber

03/14/2023, 2:49 AM
Should I file an issue about it, or is that just the way it would work with SubcomposeLayout?
b

Ben Trengrove [G]

03/14/2023, 2:50 AM
Yeah it is working as intended
e

eygraber

03/14/2023, 2:59 AM
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

Ben Trengrove [G]

03/14/2023, 3:10 AM
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

eygraber

03/14/2023, 3:15 AM
Thanks, I'll wait until tomorrow to see if anyone else has thoughts, and file a bug if there are none.
6 Views