【发布时间】:2020-01-23 18:43:42
【问题描述】:
为什么下面的签名会给出Unsupported [suspend operator "getValue"] 编译错误?
suspend operator fun getValue(thisRef: Any?, property: KProperty<*>): T
是不是因为技术限制?
【问题讨论】:
为什么下面的签名会给出Unsupported [suspend operator "getValue"] 编译错误?
suspend operator fun getValue(thisRef: Any?, property: KProperty<*>): T
是不是因为技术限制?
【问题讨论】:
getValue 运算符用于实现val 的getter,没有suspend val 这样的东西——只有普通的val。
suspend fun 不能直接从普通函数调用,例如 getter 的实现——所以这个getValue 不能用于其预期目的。 Kotlin 通过在尝试创建 suspend operator fun getValue 时给出编译错误来警告您。
【讨论】: