【问题标题】:why suspend operator fun getValue not supported in kotlin?为什么在 kotlin 中不支持暂停 operator fun getValue?
【发布时间】:2020-01-23 18:43:42
【问题描述】:

为什么下面的签名会给出Unsupported [suspend operator "getValue"] 编译错误?

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

是不是因为技术限制?

【问题讨论】:

    标签: kotlin kotlin-coroutines


    【解决方案1】:

    getValue 运算符用于实现val 的getter,没有suspend val 这样的东西——只有普通的val

    suspend fun 不能直接从普通函数调用,例如 getter 的实现——所以这个getValue 不能用于其预期目的。 Kotlin 通过在尝试创建 suspend operator fun getValue 时给出编译错误来警告您。

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2016-06-24
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多