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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With