首页 > 解决方案 > 使用协程通道时,如何激活 -> 运算符?

问题描述

在此示例中,https://kotlinlang.org/docs/channels.html#prime-numbers-with-pipeline

我们有两个函数:

fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
    var x = start
    while (true) send(x++) // infinite stream of integers from start
}
fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<Int> {
    for (x in numbers) if (x % prime != 0) send(x)
}

但是,该示例无法编译

numbersFrom(2) -> filter(2) -> filter(3) -> filter(5) -> filter(7)

我得到的编译错误是Expecting comma or ')'

有没有我忘记使用的导入。我到目前为止的进口是

import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce

标签: kotlinkotlin-coroutines

解决方案


推荐阅读