有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何为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) 个答案

  1. # 1 楼答案

    您可以使用@receiver来记录扩展函数的接收者。这是relevant documentation

    例如:

    /**
     * @receiver A String that is at least four characters long
     */
    fun String.firstFour() = this.substring(0, 4)