Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 689
Farbod Salamat-Zadeh Avatar asked Mar 10 '18 15:03

Farbod Salamat-Zadeh


People also ask

What is () -> unit in Kotlin?

Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

What is typeAlias?

A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program. Type alias do not create new types. They simply provide a new name to an existing type.


1 Answers

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.

like image 97
yole Avatar answered Sep 21 '22 15:09

yole