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

Tamas

07/15/2020, 5:38 PM
hello guys, I am fighting with intellij and coroutines. I try to use
produce
I have imported it from
import kotlinx.coroutines.channels.produce
but the ide still displays it red, and says
create function
on alt-enter. my dependencies are
Copy code
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk9:1.3.7'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-javafx:1.3.7'
o

octylFractal

07/15/2020, 5:39 PM
is
kotlinx-coroutines-jdk9
a thing? I thought there was only
jdk8
o

octylFractal

07/15/2020, 5:41 PM
Additionally, you may need an explicit dependency on
kotlinx-coroutines-core
, I don't know if the other ones declare it as
api
or
implementation
, the latter would mean
core
wouldn't appear on your classpath
t

Tamas

07/15/2020, 5:42 PM
I will try this
a

araqnid

07/15/2020, 5:45 PM
(the jdk9 flavour added interop with Java9's Flow, a reactive streams implementation)
o

octylFractal

07/15/2020, 5:46 PM
yes, I see that now
t

Tamas

07/15/2020, 5:51 PM
did not change
Copy code
fun addFile(file: File): ReceiveChannel<List<String>> = produce {
    }
the ide writes unresolved reference, but with ctrl+click I can jump to produce sources
o

octylFractal

07/15/2020, 5:52 PM
oh, I know why that's unresolved -- you don't have a
CoroutineScope
to call it on
unless that's not part of the snippet you showed
t

Tamas

07/15/2020, 5:54 PM
hmm that is it
I was watching a tutorial online - and that one does not use CoroutineScope. it is version m6 of coroutines though
o

octylFractal

07/15/2020, 5:56 PM
yes, the scoping / structured concurrency was added in the later iterations of coroutines
t

Tamas

07/15/2020, 5:57 PM
then this leads to my next question. the examples on the kotlin site are for functions, but I would like to use produce in a method of a class. how can I get the coroutine scope there
o

octylFractal

07/15/2020, 5:57 PM
pass it as a normal argument, either to the method or the constructor
receivers are just syntax sugar 🙂
t

Tamas

07/15/2020, 6:04 PM
thanks for your help! I will try further 🙂 (it is harder to wrap my head around it than I first thought)
4 Views