kotlin - 为什么委托类方法 getValue 和 setValue 需要用 operator 关键字标记?
问题描述
这是Delegated properties文档中的一个示例。
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return "$thisRef, thank you for delegating '${property.name}' to me!"
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
println("$value has been assigned to '${property.name}' in $thisRef.")
}
}
解决方案
这是因为委托属性是按约定定义的。这意味着:
[它的]语义是通过将一种句法形式句法扩展为另一种句法形式来定义的。
您可以在文档页面的下方看到委托属性的扩展:
class C {
var prop: Type by MyDelegate()
}
// this code is generated by the compiler instead:
class C {
private val prop$delegate = MyDelegate()
var prop: Type
get() = prop$delegate.getValue(this, this::prop)
set(value: Type) = prop$delegate.setValue(this, this::prop, value)
}
约定定义的语法的一个特征是(来自第一个链接):
All call expressions that are produced by expansion are only allowed to use operator functions.
and also:
This expansion of a particular syntax form to a different piece of code is usually defined in the terms of operator functions.
Just to give you more examples, the first link shows more examples of syntaxes that are defined by convention. Here are the corresponding operator functions related to them:
Syntax defined by convention | Related operator functions |
---|---|
Arithmetic and comparison operators | plus , compareTo etc |
invoke convention |
invoke |
Operator-form assignments | plusAssign , minusAssign etc |
For-loop statements | iterator , hasNext , next |
Delegated properties | setValue , getValue |
Destructuring declarations | component1 , component2 etc |
Notice that you need to put the word operator
on all of those functions for the corresponding syntax to work. In other words, "operator
" signifies that this function can be used in a syntax defined by convention.
推荐阅读
- angular - Windows 讲述人无法识别 AriaDescriber.describe() 方法描述
- bash - Shell脚本:复制一个文件夹,但只创建具有正确名称和时间戳的空文件?
- teradata - 多台显示器上的 Teradata SQL Assistant/Studio
- content-management-system - Strapi:模型配置获取期间发生错误
- javascript - Vanilla JavaScript 搜索 - 如何添加多个字段?
- c# - 使用 C# Windows 窗体进行本地化,无法访问我的 .resx 文件
- javascript - 我的谷歌图表 x 轴和 y 轴未正确显示
- c - Qualcomm Hexagon:优化 MAC-loop
- azure - 我可以在 Azure 表存储筛选器中使用请求元数据(例如主机域)吗?
- node.js - 如何从 NodeJS 服务器下载 React 中的文件?(获取文件损坏)