本文介绍了如何将暂停功能作为显式参数传递给协程生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将协程代码作为block: suspend CoroutineScope.() -> Unitlaunch协程生成器.我们通常将代码作为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)

这篇关于如何将暂停功能作为显式参数传递给协程生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:48