This message was deleted.
# compose
s
This message was deleted.
s
Mostly because of backwards compat, we didn't have the accessor scope until 1.10 roughly
Composition locals can be read outside of composition (e.g. modifier nodes), and we don't want to involve composition in those cases
Yeah, check currentValueOf in Modifier.Node No idea what ViewModel is implemented this way, might something about supporting different platforms
e
@min I already answered you here https://kotlinlang.slack.com/archives/CJLTWPH7S/p1784797026286239?thread_ts=1784715762.471929&cid=CJLTWPH7S. The way KxCoroutines providing
asSharedFlow()
API to wrap a
MutableSharedFlow
so that the underlying object type is not mutable, is the same way the
ProvidableCompositionLocal
needs to be encapsulated so that its not mutable outside of the internal library.
Change the value of the composition local.
c
To answer the original question, the example,
Copy code
val local = compositionLocalWithComputedDefaultOf {/* ... */}

CompositionLocalProvider(local provides x) {/* ... */}
In this example there is no difference between using
compositionLocalOf()
and
compositionLocalWithComputedDefaultOf()
Where the two diverges is what happens when you don't provide
local
, what is the
value
of
local
? If a
compositionLocalOf()
composition local is not provided
defaultValue
is invoked (the first time, it is cached after that) and is returned as
value
. In
compositionLocalWithComputedDefaultOf()
the compute lambda is called every time but it is also a `CompositionLocalAccessorScope`so it can read other composition locals to produce its value. An example of this use is, assume you have a holder of values (think
Context
in Android) but you want to surface parts of the holder as separate composition locals (such as default text direction). Before computed composition locals, you were required to provide each local separately. Now you can declare the individual holders as computed by default which will read the holder and return the value. Then providing a new holder all the individual locals will now report values from the new holder without requiring their being explicitly re-provided.
➕ 1
This was created to prevent tight coupling between Compose, ViewModel and Nav3.