hello guys, I am fighting with intellij and corout...
# coroutines
t
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
is
kotlinx-coroutines-jdk9
a thing? I thought there was only
jdk8
o
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
I will try this
a
(the jdk9 flavour added interop with Java9's Flow, a reactive streams implementation)
o
yes, I see that now
t
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
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
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
yes, the scoping / structured concurrency was added in the later iterations of coroutines
t
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
pass it as a normal argument, either to the method or the constructor
receivers are just syntax sugar 🙂
t
thanks for your help! I will try further 🙂 (it is harder to wrap my head around it than I first thought)