i had some code running previously which worked fine before i updated.
Now after i updated the 2 libraries in maven (kotlin-stdlib & coroutines) it gives me the following error:
"Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
[ERROR] public fun <T> CoroutineScope.async(context: CoroutineContext = ..., start: CoroutineStart = ..., block: suspend CoroutineScope.() -> ???): Deferred<???> defined in kotlinx.coroutines"
I updated the Idea Plugin aswell and im serious i have no idea what is going on there, im new to kotlin so when it is my fault somehow please explain me detailed how i can solve the problem thanks!
Just for case my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.leftwitch.kotlintest</groupId>
<artifactId>KotlinTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.3.0</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.spigotmc:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
//EDIT: I also removed the old imports btw im basically just doing:
```
fun test() = async{
delay(1000)
println("Test")
}
```
async returns Deferred<T> and to get the result T, you need to call .await() on Deferred. Check your return type. Plus you need to use an extension of CoroutineScope since standalone builders have been deprecated.
there's no top level async function anymore (was deprecated since kotlinx.coroutines 0.26.0). async is an extension function of CoroutineScope.
fun test() = GlobalScope.async {
delay(1000)
println("Test")
}
Also try to not use GlobalScope, but to use proper scope depending on the life-cycle of your component. Of course GlobalScope may be legit at some places, but it should not be your default. Here's a good reading about structured concurrency: https://medium.com/@elizarov/structured-concurrency-722d765aa952
Finally, I cannot help but notice, you're using the exact kind of pattern coroutines aims to avoid. The very purpose of Kotlin coroutines is to provide a (better) alternative to future-oriented programming.
// don't do
fun test() = GlobalScope.async { .. }
// do
suspend fun test() { }
[ERROR] public fun
CoroutineScope.async(context: CoroutineContext = ..., start: CoroutineStart = ..., block: suspend CoroutineScope.() -> ???): Deferred??> defined in kotlinx.coroutines
fun test() = async{
delay(1000)
println("Test")
}
Library async has the following signature:
<T> CoroutineScope.async
You are trying to use async without a receiver and with unknown generic type, thus provoking compilation error.