CLOVIS
04/17/2024, 5:45 PM@Composable
fun foo(): Int {
return Random.nextInt()
}
Now, my understanding is that this function won't ever rerun, since it has no inputs and reads no state. If so, great, that's what I want.
Now, I want two calls of this function in different call stacks to return the same value:
@Composable
fun bar() {
println(foo())
if (…) {
println(foo())
}
}
should both return the same value.
How can I achieve something like this?CLOVIS
04/17/2024, 5:47 PMderivedStateOf
with regular functions instead of using @Composable
?Stylianos Gakis
04/17/2024, 5:52 PMCLOVIS
04/17/2024, 5:56 PMderivedStateOf
of the result of a @Composable
function, can I?Stylianos Gakis
04/17/2024, 5:59 PMStylianos Gakis
04/17/2024, 5:59 PMCLOVIS
04/17/2024, 6:23 PMStylianos Gakis
04/17/2024, 6:31 PMCLOVIS
04/17/2024, 6:32 PMZach Klippenstein (he/him) [MOD]
04/17/2024, 6:39 PMChuck Jazdzewski [G]
04/17/2024, 8:51 PMfoo
will return different values.
Lastly, you should consider looking at https://github.com/cashapp/molecule which uses Compose for something very much like you describe. The change above, for example, would be a signficant win for Molecule.xoangon
04/23/2024, 6:35 AMWe can think of a Composable lambda likeI wonder if this would work for what you need:as being equivalently implemented as@Composable (A, B) -> C
. Callsites where the lambda is invoked (State<@Composable (A, B) -> C
) can then be replaced with the equivalentlambda(a, b)
lambda.value.invoke(a, b)
@Composable
fun bar(foo: @Composable () -> Unit) {
println(foo())
if (…) {
println(foo())
}
}
CLOVIS
04/23/2024, 7:33 AMxoangon
04/23/2024, 7:58 AMChuck Jazdzewski [G]
04/23/2024, 4:09 PMComposableLambda
instance which tracks when new lambda are created and when the lambda is executed. This allows composition skip the caller of the lambda but ensure the lambda's body is executed.
We are considering changing this in the future so that the ComposableLambda
instance is only modified when the captured values of the lambda change, which is what we do for normal lambdas today.xoangon
04/23/2024, 6:15 PMChuck Jazdzewski [G]
04/23/2024, 6:19 PMComposableLambda
can be though of as a State<T>
though it is implemented differently. This is not memoization, however.xoangon
04/23/2024, 8:06 PM