https://kotlinlang.org logo
Title
m

mkrussel

03/31/2022, 9:05 PM
How do you get the documentation for a constructor. I found a class, and used the
primaryConstructor
to get the constructor. With the following code the constructors
docString
property is null.
/**
 * A fake service that needs to be removed.
 */
@MainThread
@NotThreadSafe
public class FooService @GenerateOptions
/**
 * The constructor.
 *
 * @param other The other thing
 */
public constructor(
    /**
     * The bar.
     */
    @get:MainThread public val bar: Int,

    /**
     * The id.
     */
    @get:MainThread public val id: String? = null,
    other: String? = null,
)
j

Jiaxiang

03/31/2022, 11:37 PM
might be a bug, let me take a look later.
In your case, the
public constructor
is actually the secondary constructor for the class, so if you get the doc string for primary constructor, it is an implicit one therefore has no doc strings.
m

mkrussel

04/11/2022, 1:57 AM
I'm pretty sure that is a primary constructor. It is not declared in the body of the class, like a secondary constructor would be. When I access the primary constructor I see
other
as an parameter that it takes. It follows the syntax from https://kotlinlang.org/docs/visibility-modifiers.html#constructors for how to specify the visibility of a primary constructor.
j

Jiaxiang

04/11/2022, 1:58 AM
in your case, it should be written as
public class FooService constructor() {
to make it a primary constructor.
m

mkrussel

04/11/2022, 1:59 AM
And it is except that I added public and before it with an annotation.
j

Jiaxiang

04/11/2022, 2:00 AM
ohh, I didn’t notice it is still the same line, on my screen it is truncated to the second line making it loos like secondary constructor.
m

mkrussel

04/11/2022, 2:08 AM
It is on different lines but not inside the class body (no
{
). That should make it still be a primary constructor. Like I said everything about the constructor except for the kdoc is found with the primary constructor property of the class.
j

Jiaxiang

04/11/2022, 2:09 AM
yes it is still a primary constructor as long as it is on the class header.
I need to think about the case though, because in this case the document comment is technically on the class, shadows the primary constructor itself.