首页 > 解决方案 > 为什么在 kotlin 中不支持暂停 operator fun getValue?

问题描述

为什么以下签名会产生Unsupported [suspend operator "getValue"]编译错误?

suspend operator fun getValue(thisRef: Any?, property: KProperty<*>): T

是因为技术限制吗?

标签: kotlinkotlin-coroutines

解决方案


getValue操作符用于实现 a 的 getter,没有vala 之类的东西suspend val——只有一个普通的val

Asuspend fun不能直接从普通函数中调用,例如 getter 的实现——因此它getValue不能用于其预期目的。Kotlin 通过在尝试创建suspend operator fun getValue.


推荐阅读