如何为Kotlin扩展函数的接收者添加KDoc注释(Java中的第一个参数,`this`在Kotlin中)
考虑这个不可扩展函数:
fun checkArguments(expression: Boolean) {
if (!expression) {
throw IllegalArgumentException()
}
}
在kotlin和java中使用此函数时,可以看到其参数名:expression
我还可以编写与扩展函数相同的功能:
fun Boolean.checkArguments() {
if (!this) {
throw IllegalArgumentException()
}
}
当我以这种方式将其写入扩展函数时,调用它的布尔值的参数名(函数中的this
变量,也称为接收方)显示为$this$checkArguments
。如何为该参数添加KDoc文档注释?使用@param $this$checkArguments
似乎并没有记录它
# 1 楼答案
您可以使用
@receiver
来记录扩展函数的接收者。这是relevant documentation例如: