Kotlin:tailrec 让暂停的乐趣永无止境

Jok*_*oka 5 coroutine kotlin kotlin-coroutines

我正在学习如何使用 CompletableFuture 使协程与 Java 库一起工作。下面是我的代码:

// x invokes y invokes z invokes Java client
suspend fun x(i: Int, client: FakeJavaClient): Int {

    fun z(k: Int): Int {
        println("z: $k")
        return client.query(k).get()
    }

    tailrec // with 'tailrec', the code never terminates
    suspend fun y(j: Int): Int {
        val ret = z(j)
        if (ret > 10) {
            return ret
        }

        return y(j + 1)
    }

    return y(i)
}

fun main()  {
    runBlocking {
        launch(Dispatchers.IO) {
            FakeJavaClient().use { x(0, it) }
        }
    }
    println("Done")
}

class FakeJavaClient : AutoCloseable {
    private val executor = Executors.newFixedThreadPool(10)

    fun query(i: Int): CompletableFuture<Int> {
        val f = CompletableFuture<Int>()
        executor.submit {
            Thread.sleep(1000)
            f.complete(i * 2)
        }
        return f
    }

    override fun close() {
        executor.shutdown()
        executor.awaitTermination(10, TimeUnit.SECONDS)
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我将tailrec修饰符添加到 function y,代码输出如下所示并且永远不会结束:

z: 0
z: 0
z: 0
z: 0
z: 0
...
Run Code Online (Sandbox Code Playgroud)

如果我删除tailrecon y,代码的行为符合我的预期

z: 0
z: 1
z: 2
z: 3
z: 4
z: 5
z: 6
Done
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我了解这里发生了什么吗?

Ale*_*nov 1

这看起来像是一个已知问题:为本地 tailrec 挂起函数生成错误代码