Is it possible to rewrite the following `ViewModel...
# android
t
Is it possible to rewrite the following
ViewModel
private extensions function into a public, standalone function which can be used by all ViewModels in a project?
Copy code
private fun <E> SendChannel<E>.sendOneTimeEvent(event: E) {
    viewModelScope.launch {
        send(event)
    }
}
I thought, it might work by leveraging the experimental context receivers feature
-Xcontext-receivers
.
Copy code
context(ViewModel)
fun <E> SendChannel<E>.sendOneTimeEvent(event: E) {
    viewModelScope.launch {
        send(event)
    }
}
But this raises an error at call site: > No required context receiver found: Cxt { context(androidx.lifecycle.ViewModel) public fun E kotlinx.coroutines.channels.SendChannelE.sendOneTimeEvent(event: E)
d
Something about doing a
launch
just for a
send
seems incorrect to me. Why not just have an unbounded channel and use
trySend
?
Then you wouldn't even need the
viewModelScope
.