https://kotlinlang.org logo
Title
d

Daniele Segato

12/18/2020, 10:04 AM
Noob question. I'm using Android Studio (android development) I've Included dependencies:
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
But I cannot use
async
and
launch
in a
suspend fun
suspend fun foo() {
  async {} // Unresolved reference
  launch {} // Unresolved reference
}
What am I missing here?! EDIT: nevermind, I was remembering it wronly, I need something like
coroutineScope {}
or
withContext(...)
or something like that
Anyone?
m

Milan Hruban

12/18/2020, 10:15 AM
async and launch are extension functions on CoroutineScope - you cannot call them without it
d

Daniele Segato

12/18/2020, 10:15 AM
I'm inside a
suspend fun
w

wbertan

12/18/2020, 10:17 AM
Are you in the same module which that is imported?
d

Daniele Segato

12/18/2020, 10:17 AM
yes, it's an android app
w

wbertan

12/18/2020, 10:18 AM
I haven’t use that dependency, we use here:
"org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}"
d

Daniele Segato

12/18/2020, 10:19 AM
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2"
buildscript {
    ext.kotlin_version = '1.4.21'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha03'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 
    }
}
org.jetbrains.kotlinx:kotlinx-coroutines-android
should include
org.jetbrains.kotlinx:kotlinx-coroutines-core
but I manually included core just in case, no difference
m

Milan Hruban

12/18/2020, 10:21 AM
Why does it matter that you are inside suspend function?
d

Daniele Segato

12/18/2020, 10:21 AM
cause the scope is that coroutine? it is supposed to create a child coroutine
otherwise structured concurrency goes out of the window
When I used coroutine 1.3.8 I could use
async
and
launch
just fine within a
suspend fun
m

Milan Hruban

12/18/2020, 10:24 AM
oh is it some android thing then?
d

Daniele Segato

12/18/2020, 10:24 AM
why would it be? this is Kotlin, as far as I know there's no "android" kotlin, there's just kotlin
m

Milan Hruban

12/18/2020, 10:25 AM
Can you please point me to some resource showing this? I am a bit lost
d

Daniele Segato

12/18/2020, 10:25 AM
I've no idea what you are asking about @Milan Hruban
I do not see any change in regard to that here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/CHANGES.md
m

Milan Hruban

12/18/2020, 10:29 AM
@Daniele Segato some code sample ideally in kotlin docs, that shows usage of
async
or
launch
in suspend function, without coroutine scope
d

Daniele Segato

12/18/2020, 10:34 AM

https://youtu.be/Mj5P47F6nJg?t=2208

is this good enough?
are you saying I should do
coroutineScope {
            launch() {}
            async() {}
        }
?
or
withContext
?
maybe I'm wrong but I remember using it directly inside the
suspend fun
you are saying this was never possible?
v

Vampire

12/18/2020, 10:40 AM
d

Daniele Segato

12/18/2020, 10:40 AM
I checked coroutines a while ago, studied it than had to do something else and now i'm getting back to it. I still didn't build hours of work on it
m

Milan Hruban

12/18/2020, 10:40 AM
well it depends what you want to do - the docs will probably explain it better then me
d

Daniele Segato

12/18/2020, 10:44 AM
I see, I was probably remembering wrongly... I always used some kind of container for that.... so now I've another question about testing, doing it in another thread.
e

elizarov

12/18/2020, 11:02 AM
It was possible more that 2 years ago, before 1.0 release -> https://elizarov.medium.com/structured-concurrency-722d765aa952 (it also explains why it is not allowed now)
d

Daniele Segato

12/18/2020, 11:05 AM
@elizarov ye, sorry, I actually rewatched that part and realized it... My bad, unfurtunately I spent time to study coroutines and try them out a couple of months ago, than I suspended cause I needed SharedFlow and needed to finish a project. Now I'm taking it up again more seriously and I just miss-remembered that. Thanks for your answer!