Service functions returning flows must not be susp...
# kotlinx-rpc
s
Service functions returning flows must not be suspending anymore? https://kotlin.github.io/kotlinx-rpc/features.html#send-and-receive-flows Why is this a requirement, it's rather disturbing when my flow comes from a suspend call like a db. and what's the best way to handle such cases?
c
suspend () -> Flow<T>
is always a code smell, it's code that suspends in two different ways
If you need to initialize something before processing elements, do so within the flow:
Copy code
fun foo(): Flow<T> = flow {
    // initialize your stuff
    emitAll(/* your request */)
}
s
isn't emitAll suspending as well?
c
Yes. Within
flow {}
you can suspend, so it's not a problem.
s
ohh.. well that changes things, almost all my functions here are suspending so I never even noticed
c
It's a common mistake, yeah…
Flow<T>
means "a suspending function that returns multiple values", so
suspend () -> Flow<T>
is "a suspending function which returns a suspending function which returns multiple values", which is repetitive and confusing, because you don't know what happens at each of the suspensions
kinda of similar to why a
suspend
function shouldn't have a
CoroutineScope
receiver
s
yeah that makes sense. I suppose I have a lot of code to change. thanks!
c
No worries! Good luck!
As a temporary solution, you can use:
Copy code
fun <T> realFlow(block: suspend () -> Flow<T>) = flow {
    emitAll(block())
}
to adapt between your old functions and the well-written ones
👍🏻 1
👍 1
a
Exactly, thank you Ivan for answering this!