Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将任何乐趣转换为具有高lamba表达式的暂停乐趣_Android_Kotlin_Functional Programming - Fatal编程技术网

Android 将任何乐趣转换为具有高lamba表达式的暂停乐趣

Android 将任何乐趣转换为具有高lamba表达式的暂停乐趣,android,kotlin,functional-programming,Android,Kotlin,Functional Programming,我正在寻找一种方法,将任何函数作为参数发送,并返回一个挂起的函数 这是一个可能函数的示例,以及我如何解决此问题: 可能的功能 fun getSomething(): List<SomethingObject> fun getSomething():列表 错误接近 suspend fun AnyClass.awaitAll(): List<SomethingObject> = withContext(Dispatchers.IO) { getSomething() }

我正在寻找一种方法,将任何函数作为参数发送,并返回一个挂起的函数 这是一个可能函数的示例,以及我如何解决此问题: 可能的功能

fun getSomething(): List<SomethingObject>
fun getSomething():列表
错误接近

suspend fun AnyClass.awaitAll(): List<SomethingObject> = withContext(Dispatchers.IO) { getSomething() }
suspend-fun-AnyClass.awaitAll():List=withContext(Dispatchers.IO){getSomething()}
但我真正想要的是这样的东西:

suspend fun <T : Any?> safeQuery(query: () -> Unit): () -> Unit {
     return withContext(Dispatchers.IO) { query }
}
suspend-fun-safeQuery(查询:()->单位):()->单位{
返回withContext(Dispatchers.IO){query}
}
Soo…类似于:

suspend fun <T : Any?> safeQuery(query: (T) -> Unit): Unit {
     return withContext(Dispatchers.IO) { query }
}
suspend-fun-safeQuery(查询:(T)->单位):单位{
返回withContext(Dispatchers.IO){query}
}

suspend-fun-safeQuery(查询:()->T):T{
返回withContext(Dispatchers.IO){query}
}

这是否回答了您的问题?

我对协同程序没有经验,但我看到的一个错误是您没有调用作为参数传递的函数。它应该是
query()
而不是
query
。我想将一个函数传递给另一个函数,并返回同一个挂起的函数
suspend fun <T : Any?> safeQuery(query: () -> T): T {
     return withContext(Dispatchers.IO) { query }
}