嵌套函数未在声明位置执行

时间:2019-06-21 07:37:57

标签: android kotlin kotlin-coroutines

在Android应用中。

  suspend fun getTraidersList(isCustomtHandle: Boolean = false): Any {

            suspend fun execOperation() = traderMonitorRestClient.getTraidersList()
            return runOperationWithoutHandle(::execOperation) // real execute here
}

    suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
            val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
            response
}

如您在getTraidersList中所见,我使用了嵌套的一行功能

suspend fun execOperation() = traderMonitorRestClient.getTraidersList()

traderMonitorRestClient.getTraidersList()的真正执行在线上

runOperationWithoutHandle(::execOperation)

问题是: 为什么traderMonitorRestClient.getTraidersList()在声明处不执行

suspend fun execOperation() = traderMonitorRestClient.getTraidersList()

0 个答案:

没有答案