Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
13.03k stars 1.85k forks source link

cannot be cast to class kotlinx.coroutines.flow.FlowCollector #3482

Closed peterpilgrim closed 2 years ago

peterpilgrim commented 2 years ago

Hello. Greetings.

I am using Kotlin 1.6.4 and playing about with the Arrows Flow https://kotlinlang.org/docs/flow.html#flows

I tried to run this example:

package uk.co.xenonique.learn;

import arrow.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

/*
 * See here
 * https://kotlinlang.org/docs/flow.html#flows
 * https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-flow-04.kt
 *
 */
fun simple123(): Flow<Int> = flow { // flow builder
    for (i in 1..3) {
        delay(100) // pretend we are doing something useful here
        emit(i) // emit next value
    }
}

fun main() = runBlocking<Unit> {
    // Launch a concurrent coroutine to check if the main thread is blocked
    launch {
        for (k in 1..3) {
            println("I'm not blocked $k")
            delay(100)
        }
    }
    // Collect the flow
    simple123().collect { value -> println(value) }
}

But in both IntelliJ and Gradle CLI I get this ClassCastException.

/Users/peterpilgrim/.sdkman/candidates/java/17.0.4-tem/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61958:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/peterpilgrim/Projects/personal/arrow-kt-learning/build/classes/kotlin/main:/Users/peterpilgrim/.gradle/caches/7.4/generated-gradle-jars/gradle-api-7.4.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-ant-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-astbuilder-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-console-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-datetime-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-dateutil-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-groovydoc-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-json-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-nio-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-sql-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-templates-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-test-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/groovy-xml-3.0.9.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/javaparser-core-3.17.0.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/kotlin-stdlib-1.5.31.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/kotlin-stdlib-common-1.5.31.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/kotlin-stdlib-jdk7-1.5.31.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/kotlin-stdlib-jdk8-1.5.31.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/kotlin-reflect-1.5.31.jar:/Users/peterpilgrim/.gradle/wrapper/dists/gradle-7.4-bin/c0gwcg53nkjbqw7r0h0umtfvt/gradle-7.4/lib/gradle-installation-beacon-7.4.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.6.21/eeb4d60d75e9ea9c11200d52974e522793b14fba/kotlin-stdlib-jdk8-1.6.21.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.6.10/1cbe9c92c12a94eea200d23c2bbaedaf3daf5132/kotlin-reflect-1.6.10.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.6.21/11ef67f1900634fd951bad28c53ec957fabbe5b8/kotlin-stdlib-1.6.21.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.6.4/2c997cd1c0ef33f3e751d3831929aeff1390cb30/kotlinx-coroutines-core-jvm-1.6.4.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.6.21/568c1b78a8e17a4f35b31f0a74e2916095ed74c2/kotlin-stdlib-jdk7-1.6.21.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.6.21/5e5b55c26dbc80372a920aef60eb774b714559b8/kotlin-stdlib-common-1.6.21.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/io.arrow-kt/arrow-core-jvm/1.1.2/ee1726190f389bdf38bed472fe6a10cae565ca46/arrow-core-jvm-1.1.2.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/io.arrow-kt/arrow-annotations-jvm/1.1.2/5f489b336138baa0cbfa527a7441a229066097c2/arrow-annotations-jvm-1.1.2.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/io.arrow-kt/arrow-continuations-jvm/1.1.2/2b69ea3c70380f51b96a7a382050ef891aa2044d/arrow-continuations-jvm-1.1.2.jar:/Users/peterpilgrim/.gradle/caches/modules-2/files-2.1/org.codehaus.mojo/animal-sniffer-annotations/1.21/419a9acd297cb6fe6f91b982d909f2c20e9fa5c0/animal-sniffer-annotations-1.21.jar uk.co.xenonique.learn.Demo_flows_01Kt
Exception in thread "main" java.lang.ClassCastException: class uk.co.xenonique.learn.Demo_flows_01Kt$main$1$2 cannot be cast to class kotlinx.coroutines.flow.FlowCollector (uk.co.xenonique.learn.Demo_flows_01Kt$main$1$2 and kotlinx.coroutines.flow.FlowCollector are in unnamed module of loader 'app')
    at uk.co.xenonique.learn.Demo_flows_01Kt$main$1.invokeSuspend(demo-flows-01.kt:29)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
    at uk.co.xenonique.learn.Demo_flows_01Kt.main(demo-flows-01.kt:20)
    at uk.co.xenonique.learn.Demo_flows_01Kt.main(demo-flows-01.kt)

I am not sure what is going on?

dkhalanskyjb commented 2 years ago

Hello. If I remove import arrow.core.*, the code executes without an error, so this is not an issue with the coroutine library. Arrow is not an official part of Kotlin, it's a third-party plugin. I suggest asking someone knowledgeable about Arrow about this. Probably someone on the Kotlin Slack channel can answer this. Or maybe simply not using Arrow is also an option in your case.

peterpilgrim commented 2 years ago

I removed the arrow.core.* but I still get the error. I also copied the code to another brand new project, which was clean, without the arrows dependencies. I still got the error. I am running OpenJDK 17 temurin, perhaps, this is modules or something?

dkhalanskyjb commented 2 years ago

Please share a full project where this issue reproduces.

peterpilgrim commented 2 years ago

Sure. Here goes https://github.com/peterpilgrim/coroutines-kt-learn-temp

dkhalanskyjb commented 2 years ago

Your project uses an old version of the compiler. Only Kotlin > 1.7.0 1.6.0 is supported in coroutines 1.6.0 and above. When I replace

    `kotlin-dsl`

in the plugins block in your build.gradle.kts with

    kotlin("jvm") version "1.7.20"

everything starts to work.

peterpilgrim commented 2 years ago

Good heavens!

Thank you very much @dkhalanskyjb for investing your time and effort. Yes it does work.

dkhalanskyjb commented 2 years ago

Sorry, I was a bit mistaken: Kotlin version 1.6.0 is also fine.

dkhalanskyjb commented 2 years ago

Filed an issue about the error message not being helpful: https://youtrack.jetbrains.com/issue/KT-54494/Check-that-the-compiler-complains-about-signatures-with-unknown-metadata