João Paulo Gomes
05/30/2024, 1:22 PMAnonymike
05/30/2024, 1:50 PMsimon.vergauwen
05/30/2024, 1:51 PMSharedFlow<A>
instead of AtomicReference<A>
. To the outside world you want to only show SharedFlow<A>
.
But internal in your class, you need to be able to update its state. Which you can using MutableSharedFlow<A>
to update the value inside using update { prev -> calculateNew(prev) }
and set(value)
just like you would on an AtomicReference
.
Where:
interface MutableSharedFlow<A> : SharedFlow<A> {
fun update(block: (A) -> A)
fun set(value: A)
}
So inside of your class you can now define that in a single step.
val state: SharedFlow<A>
field: MutableShareFlow(...)
Again, this is a really uncommon pattern on servers-side. Only if you're building mutable state machines this might be useful, which is what everyone on Android is doing.simon.vergauwen
05/30/2024, 1:51 PMWow, I'm glad you mentioned this as I completely missed this feature and could use it heavily as we have a lot of Flows and other classes that everytime we use them we only want to expose immutable versions of them.Are you using this on the server? 🤯 What kind of use-cases? I am assuming not for simple request<->response.
Anonymike
05/30/2024, 2:06 PMAnonymike
05/30/2024, 2:08 PMsimon.vergauwen
05/30/2024, 2:20 PMAnonymike
05/30/2024, 2:42 PMCLOVIS
05/30/2024, 3:02 PMJacob
05/30/2024, 3:25 PMJacob
05/30/2024, 3:28 PMAnonymike
05/30/2024, 3:30 PMAnonymike
05/30/2024, 3:32 PMAnonymike
05/30/2024, 3:35 PMAnonymike
05/30/2024, 11:28 PMkotlin {
sourceSets.all {
languageSettings.enableLanguageFeature("ExplicitBackingFields")
}
}
https://stackoverflow.com/questions/78529749/explicit-backing-field-not-working-in-latest-kotlin-2-0-0-android