考虑以下非扩展功能:
fun checkArguments(expression: Boolean) {
if (!expression) {
throw IllegalArgumentException()
}
}
当我在kotlin和java中使用此函数时,可以看到其参数名称:expression
。我还可以编写与扩展功能相同的功能:
fun Boolean.checkArguments() {
if (!this) {
throw IllegalArgumentException()
}
}
当我以此方式将其编写为扩展函数时,调用它的 bool(boolean) 值的参数名称(函数内的this
变量,又称为接收器)显示为$this$checkArguments
。如何为该参数添加KDoc文档注释?使用@param $this$checkArguments
似乎无法对其进行记录。
最佳答案
您可以使用@receiver
来记录扩展功能的接收者。这是relevant documentation。
例如:
/**
* @receiver A String that is at least four characters long
*/
fun String.firstFour() = this.substring(0, 4)
关于java - 如何为Kotlin扩展功能的接收者添加KDoc注释(Java中的第一个参数,Kotlin中的 `this`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64327122/