我试图在另一个挂起函数的参数中调用一个挂起函数。编译器实际上不允许这样做。它告诉我必须从挂起函数或协程调用挂起函数。
suspend fun compareElements(
isReady: Boolean = isReady() // IDE complains.
) {
...
}
//This is for this questions purpose. Reality is a bit more complex.
suspend fun isReady() = true
我怎样才能做到这一点?我需要
isReady()
在参数中。
最佳答案
您可以将挂起函数作为默认参数传递:
suspend fun compareElements(
readyCheck: suspend () -> Boolean = { isReady() }
) {
if (readyCheck()) {
...
}
}
关于kotlin - 如何从函数调用挂起函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441979/