应该如何记录 Kotlin 函数类型化?
How should Kotlin function typealises be documented?
在 Kotlin v1.1+ 中,有声明 type aliases 的选项,它为现有类型提供替代名称。这对于函数类型特别有用——例如:
typealias OnItemClick = (view: View, position: Int) -> Boolean
他们可以像其他成员一样用 KDoc 评论记录:
/**
* Type definition for an action to be preformed when a view in the list has been clicked.
*/
typealias OnItemClick = (view: View, position: Int) -> Boolean
但是有没有一种特定的方法来记录函数类型的参数和return类型?
Kotlin 网站提供了有关 documenting Kotlin code 的信息,但没有提及类型别名。
就像函数本身一样,如果函数类型可以这样记录就更好了:
/**
* @param view the view that was clicked
* @param position the layout position from the ViewHolder (see
[ViewHolder.getLayoutPosition])
* @return whether the click was successful
*/
typealias OnItemClick = (view: View, position: Int) -> Boolean
但是 KDoc 无法识别标签。
那么应该如何记录参数和 return 类型?
不幸的是,此时 KDoc 没有特别支持记录参数和 return 函数类型的类型别名的值,所以您只需要将它们描述为文档的一部分。我已提交 feature request 以添加支持。
在 Kotlin v1.1+ 中,有声明 type aliases 的选项,它为现有类型提供替代名称。这对于函数类型特别有用——例如:
typealias OnItemClick = (view: View, position: Int) -> Boolean
他们可以像其他成员一样用 KDoc 评论记录:
/**
* Type definition for an action to be preformed when a view in the list has been clicked.
*/
typealias OnItemClick = (view: View, position: Int) -> Boolean
但是有没有一种特定的方法来记录函数类型的参数和return类型?
Kotlin 网站提供了有关 documenting Kotlin code 的信息,但没有提及类型别名。
就像函数本身一样,如果函数类型可以这样记录就更好了:
/**
* @param view the view that was clicked
* @param position the layout position from the ViewHolder (see
[ViewHolder.getLayoutPosition])
* @return whether the click was successful
*/
typealias OnItemClick = (view: View, position: Int) -> Boolean
但是 KDoc 无法识别标签。
那么应该如何记录参数和 return 类型?
不幸的是,此时 KDoc 没有特别支持记录参数和 return 函数类型的类型别名的值,所以您只需要将它们描述为文档的一部分。我已提交 feature request 以添加支持。