鉴于一些
suspend fun a(): Int
这有效:
launch(Unconfined) {
(1..10).forEach {
val a = a()
println("Result is $a")
}
}
但这在编译时失败:
val action: (Int) -> Unit = {
// Suspend function should be called only from a coroutine
// or another suspend function:
val a = a()
println("Result is $a")
}
launch(Unconfined) {
(1..10).forEach(action)
}
此外,它无法修复,因为:
val action: suspend (Int) -> Unit = {
val a = a()
println("Result is $a")
}
launch(Unconfined) {
// suspend (Int) -> Unit cannot be applied to (T) -> Unit
(1..10).forEach(action)
}
就静态类型系统而言,这里的故事是什么?当前情况看起来像是一个快速破解,其中包含suspend fun
调用的内联块仍被推断为非挂起类型签名。
这是一个在最终确定之前需要改进设计的领域吗?