https://kotlinlang.org logo
Title
w

William Reed

07/02/2021, 12:05 PM
short of writing my own extension function, is there any more concise way of writing
flow {
    emit(suspendFunction())
}
I know I could use
flowOf(…)
but the call site is not in a suspend function already
a

alexandrepiveteau

07/02/2021, 1:22 PM
Something like
::suspendFunction.asFlow()
?
🆒 5
w

William Reed

07/02/2021, 1:26 PM
woah didn’t know that existed
neat
m

Mark

07/04/2021, 3:31 AM
Somewhat related, for suspending functions (or lambdas) that return a
Flow
I use these convenience functions:
fun <T> suspendAsFlow(flowLambda: suspend () -> Flow<T>) = flowLambda.asFlow()

fun <T> (suspend () -> Flow<T>).asFlow(): Flow<T> = flow {
    val flow = this@asFlow()
    emitAll(flow)
}
u

ursus

07/04/2021, 12:15 PM
the .asFlow is useless if you have external params. I really wish the library provided this. Everybody then calls it differently, mine is
simpleFlow { }