statmark56
10/19/2023, 3:36 PMsuspend fun foo(): Flow<T>
I once read if a fun returns Flow, it shouldn't marked suspend. But I cannot remember what could go wrong with it. Anyone?Joffrey
10/19/2023, 3:38 PMsuspend
function is expected to do all its work before returning
• a function returning Flow
is supposed to return quickly without doing anything, because the Flow
collection later on is supposed to trigger the work (and is itself suspend)
So if a function has both suspend
and Flow
return type, it's unclear what its contract is (and it should be documented). What's your use case?streetsofboston
10/19/2023, 3:40 PMJoffrey
10/19/2023, 3:42 PMfunctionReturningFlow().collect { ... }
and suspendFunction()
, not between functionReturningFlow()
and suspendFunction()
.Joffrey
10/19/2023, 3:43 PMfunctionReturningFlow()
is not suspending, but collect()
isstreetsofboston
10/19/2023, 3:51 PMRobert Williams
10/19/2023, 3:55 PM{
someSuspendingFun()
return someFlow
}
into
flow {
someSuspendingFun()
emitAll(someFlow)
}
which doesn't require suspend so is simpler for consumersJacob
10/19/2023, 6:20 PMJacob
10/19/2023, 6:21 PMJoffrey
10/19/2023, 7:39 PMfun getSomething(): Deferred<X>
is not great unless the scope is extremely clear from the context. But why complicate things for a single value? The whole point of coroutines is that they are simple to use thanks to suspend functionsstreetsofboston
10/19/2023, 7:42 PMfun CoroutineScope.getSomething() : Deferred<X>
Jacob
10/19/2023, 7:52 PMstreetsofboston
10/19/2023, 7:53 PMJoffrey
10/19/2023, 8:19 PMstatmark56
10/20/2023, 2:44 AM