https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

Kshitij Patil

02/25/2021, 9:52 AM
what’s the difference between applying
conflate
on
Flow<T>
and using
StateFlow<T>
? Also, why can’t I just get a
StateFlow
when I use
map
on existing
StateFlow
?
s

streetsofboston

02/25/2021, 12:55 PM
Because map is already defined on a Flow and there it returns another Flow. A StateFlow is Flow, and since we don't want to change the api of Flow, that means that map on a StateFlow must return a Flow as well. You can create an extension method called something like mapWithState on a StateFlow to do what you want.
z

Zach Klippenstein (he/him) [MOD]

02/25/2021, 4:02 PM
Roman explained why
map
on
StateFlow
doesn’t make sense here
k

Kshitij Patil

02/25/2021, 6:06 PM
I do have the exact same use case you mentioned in the First comment there. Is there any problem with the approach you shared ? IE,
state.map{ }.stateIn(scope)
?
z

Zach Klippenstein (he/him) [MOD]

02/25/2021, 6:15 PM
No problem with it, as long as you have a scope
14 Views