Search code examples
kotlintype-aliaskdoc

How should Kotlin function typealises be documented?


In Kotlin v1.1+, there is the option of declaring type aliases, which provide alternative names for existing types. This is particularly useful for function types - for example:

typealias OnItemClick = (view: View, position: Int) -> Boolean

And they can be documented with KDoc comments, like other members:

/**
 * Type definition for an action to be preformed when a view in the list has been clicked.
 */
typealias OnItemClick = (view: View, position: Int) -> Boolean

But is there a specific way of documenting the parameters and return type of the function type?

The Kotlin website provides information on documenting Kotlin code, but makes no mention of typealiases.

Like functions themselves, it would be nice if function types could be documented like this:

/**
 * @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

But the tags aren't recognised in KDoc.

So how should the parameters and return types be documented?


Solution

  • Unfortunately at this time there is no special support in KDoc for documenting parameter and return values for typealiases to function types, so you just need to describe them as part of the documentation. I've filed a feature request to add the support.