Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document attributes in Kotlin data class?

Where shall I put Javadoc for attributes in Kotlin data class?

In other words, how to write in Kotlin the following Java code:

/**
 * Represents a person.
 */
public class Person {
    /**
     * First name. -- where to place this documentation in Kotlin?
     */
    private final String firstName;
    /**
     * Last name. -- where to place this documentation in Kotlin?
     */
    private final String lastName;

    // a lot of boilerplate Java code - getters, equals, hashCode, ...
}

In Kotlin it looks like this:

/**
 * Represents a person.
 */
data class Person(val firstName: String, val lastName: String)

but where to put the attributes' documentation?

like image 778
Honza Zidek Avatar asked Apr 05 '18 14:04

Honza Zidek


People also ask

Can data class have functions Kotlin?

Data classes specialize in holding data. The Kotlin compiler automatically generates the following functionality for them: A correct, complete, and readable toString() method. Value equality-based equals() and hashCode() methods.

Does Javadoc work with Kotlin?

Just like Kotlin, Dokka fully supports cross-language Java/Kotlin projects. It can read Javadoc comments in Java code and KDoc comments in Kotlin code and generate documentation covering the entire API of a module, regardless of the language used to write each class in it.


1 Answers

As described in the documentation, you can use the @property tag for this:

/**
 * Represents a person.
 * @property firstName The first name.
 * @property lastName The last name.
 */
data class Person(val firstName: String, val lastName: String)

Alternatively, simply mention the property names in the description of the class, if you don't have much to say about them in the docs:

/**
 * Represents a person, with the given [firstName] and [lastName].
 */
data class Person(val firstName: String, val lastName: String)
like image 137
yole Avatar answered Sep 22 '22 15:09

yole