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

elizarov

08/31/2017, 12:45 PM
@dragas Can you, please, add a self-contained example that crashes?
d

dragas

08/31/2017, 1:00 PM
Copy code
fun main(args: Array<String>) = runBlocking()
{
    val actor = getIntActor()
    delay(1000)
}

fun getIntActor() = actor<Int>(CommonPool)
{
    consumeEach(::consumeInt)
}

fun consumeInt(it : Int)
{
    println(it)
}
e

elizarov

08/31/2017, 2:20 PM
Works for me (with
kotlinx.coroutines
version 0.18)
What’s your setup where it does not work?
d

dragas

09/01/2017, 7:36 AM
I am also using
kotlinx.coroutines 0.18
and
kotlin 1.1.4-2
in a multi module gradle project, And it doesn't work anywhere, really. Be it the example I provided, or deep in the project. This is the only place I tried using actors at. https://pastebin.com/pgMgydRK
for(response in channel)
doesn't throw that and seems to work as intended.
Using
consumeEach
on regular channels throws that too, but with
Exception in thread "kotlinx.coroutines.DefaultExecutor" Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Boolean
Copy code
fun main(args: Array<String>) = runBlocking()
{
    val channel = Channel<Int>(Channel.UNLIMITED)

    channel.consumeEach { println(it) }

    delay(1000)
}

fun consumeInt(it: Int)
{
    println(it)
}
e

elizarov

09/01/2017, 7:45 AM
Can you share your project setup?
(via github, for example)
e

elizarov

09/01/2017, 4:53 PM
I’m lost at how to reproduce it. Here is what I’m doing: * git clone https://github.com/Dragas/bIRC.git -b async-dev * Opened this project in IDEA (2017.2.3, Kotlin plugin 1.1.4-3) * Imported Gradle project, configured JDK to 1.8.0_131 * Navigated to async/src/test * Created file named
T.kt
with the code above of your, pressed Alt+ENTER many times to add all the imports * Now when I run it it just works, does not crash
Here is the full code I’m running with all the imports (in
T.kt
):
Copy code
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.channels.consumeEach
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.runBlocking

fun main(args: Array<String>) = runBlocking()
{
    val actor = getIntActor()
    delay(1000)
}

fun getIntActor() = actor<Int>(CommonPool)
{
    consumeEach(::consumeInt)
}

fun consumeInt(it : Int)
{
    println(it)
}
You should try clean/rebuild project. Maybe something broken in some caches. You can try “Invalidate caches / Restart” action
d

dragas

09/01/2017, 5:51 PM
Yep, that helped. Sorry to bother you for it 😞
4 Views