Kotlin中的代码片段

  public actual fun <R, T> (suspend R.() -> T).createCoroutineUnintercepted(
    receiver: R,
    completion: Continuation<T>
): Continuation<Unit> {
    val probeCompletion = probeCoroutineCreated(completion)
    return if (this is BaseContinuationImpl)
        create(receiver, probeCompletion)
    else {
        createCoroutineFromSuspendFunction(probeCompletion) {
            (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, it)
        }
    }
}

我想知道的是如何识别(suspend R.() -> T)Function Type

任何帮助都将不胜感激.

推荐答案

没有单独的函数类型和类类型.函数类型只是可以使用特定参数和特定返回类型执行的类型.它们可与Function0Function1等接口互换,并包含单个invoke()功能.

我们可以通过类实现函数类型:

class MyClass : (suspend () -> Unit) {
    override suspend fun invoke() {}
}

现在,让我们获得以下代码:

val lambda: (suspend () -> Unit) = {}

分解后,我们看到我们的lambda被编译为:

final class FooKt$foo$lambda$1 extends kotlin/coroutines/jvm/internal/SuspendLambda implements kotlin/jvm/functions/Function1 {
    ...
    public final invoke(Ljava/lang/Object;)Ljava/lang/Object;
    ...
}

它扩展了BaseContinuationImpl的子类型SuspendLambda.它还实现了Function1,包含invoke个函数,使其成为函数类型.

Kotlin相关问答推荐

直接从SON解析NonEmptyList

API迁移到Spring Boot 3后,Spring Security无法工作

解决Microronaut中多个可能的Bean候选者冲突

如何修改muableStateMapOf的值?

在Spring Boot应用程序中使用网络请求功能将关键字挂起作为冗余

Kotlin 复制列表中的项目以创建具有相同数据的不同对象的新列表

在 detekt 配置文件中找不到某些属性

Kotlin:我可以将函数分配给 main 的伴随对象中的变量吗?

如何使用 Kotlin KClass 属性 simpleName 生成空值

比较 Kotlin 中的可比对象列表

如何在 Kotlin 中使用 volatile

使用Dagger 2提供函数依赖性

使用 Kotlin 创建自定义 Dagger 2 范围

(kotlin的Moshi)@Json vs@field:Json

这是 Kotlin 中的错误还是我遗漏了什么?

在android的默认浏览器 Select 列表中添加我的浏览器?

lateinit 的 isInitialized 属性在伴随对象中不起作用

递归方法调用在 kotlin 中导致 StackOverFlowError 但在 java 中没有

Kotlin:在何时使用枚举

在 intelliJ 元素中集成 Kotlinx 协程