Question: Why would this be preferred ```private v...
# coroutines
s
Question: Why would this be preferred
Copy code
private val _myFlow = MutableStateFlow<String>("")
val myFlow: SharedFlow<String> = _myFlow.asStateFlow()
over
Copy code
private val _myFlow = MutableStateFlow<String>("")
val myFlow: SharedFlow<String> = _myFlow
? A MutableStateFlow is a StateFlow; why the extra call to
asStateFlow()
?
c
I'm not 100% sure but I think this makes sure it can't be casted into a
MutableStateFlow
by the consumer
l
Yes, that's what it does.
s
Why would a consumer try to unsafely downcast it back to a MutableStateFlow...? Isn't that asking for problems. Consumers should just consume it, not try to find a way to write/update it.
c
Yeah if you are responsible this isn't really an issue. This is just to guard against anyone who tries to do that
👍 1
a
yea exactly, it's just defensive programming, so that somebody down the line doesn't mess it up (probably you, if my experience is anything to go by)