如何将挂起函数作为显式参数传递给协程构建器? [英] how to pass suspend function as explicit parameter to coroutine builder?
本文介绍了如何将挂起函数作为显式参数传递给协程构建器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究 launch
协程构建器,它将协程代码作为 block: suspend CoroutineScope.() ->单位
.我们通常将代码作为 lambda 传递.但是,我想知道如何将此函数作为显式参数传递给启动函数.
I'm looking into launch
coroutine builder which takes coroutine code as block: suspend CoroutineScope.() -> Unit
. We usually pass the code as lambda. However, I was wondering how to pass this function as explicit parameter to launch function.
coroutineScope {
launch(block = ::myFunction)
}
suspend fun CoroutineScope.myFunction(): Unit {
// coroutine code
}
出现以下错误
Type mismatch.
Required:
suspend CoroutineScope.() → Unit
Found:
KSuspendFunction0<Unit>
我错过了什么?
推荐答案
扩展函数引用的语法与成员函数相同:
The syntax for extension function references is the same as for member functions:
launch(block = CoroutineScope::myFunction)
这篇关于如何将挂起函数作为显式参数传递给协程构建器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文