https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rook

08/23/2019, 7:55 PM
I have the following:
Copy code
suspend fun fooFlow(): Flow<List<Foo>>
fun CoroutineScope.onFooEmission(action: (List<Foo>) -> Unit) {
  launch {
    fooFlow().collect { foos -> action(foos) }
  }
}
But at the collect call-site, I’m getting
Copy code
Type mismatch.
Required: FlowCollector<List<Foo>>
Found: (Nothing) -> Unit
s

streetsofboston

08/23/2019, 7:57 PM
collect
is a suspending function….
onFooEmission
must be suspending as well… or create a Coroutine inside
onFooEmission
and call
collect
from that Coroutine
r

rook

08/23/2019, 7:59 PM
My apologies,
onFooEmission
is a
CoroutineScope
extension and
fooFlow()
is a
suspend
s

streetsofboston

08/23/2019, 8:00 PM
You’d still need to call
launch
first, though…
Copy code
fun CoroutineScope.onFooEmission(action: (List<Foo>) -> Unit) {
  launch { fooFlow().collect { foos -> action(foos) } }
}
r

rook

08/23/2019, 8:01 PM
You’re right, I accidentally omitted a bunch of things. But that still doesn’t resolve my issue
I still have the same error with everything you outlined above
s

streetsofboston

08/23/2019, 8:03 PM
It works fine in Android Studio:
Copy code
fun fooFlow(): Flow<List<Foo>> = TODO()

fun CoroutineScope.onFooEmission(action: (List<Foo>) -> Unit) {
    launch {
        fooFlow().collect { foos ->
            action(foos)
        }
    }
}
Be sure to
import kotlinx.coroutines.flow.collect
, though!
r

rook

08/23/2019, 8:13 PM
Looks like my auto-import just completely failed. Manually importing the dependency worked just fine. Thanks!
d

David Glasser

08/24/2019, 5:01 AM
Yeah, getting extension functions to auto-import when they have the same name as interface functions is a big pain. I guess this is why star imports exist, though my team tries to avoid them.
4 Views