Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Documentation for property setter

I am writing a Kotlin library. In one of the classes, I have the following:

class SessionWrapper {

    /**
     * The time in milliseconds after which the session will expire.
     */
    var expiryTime = DEFAULT_EXPIRY_TIME
        get() {
            mainThreadCheck()
            return field
        }
        set(value) {
            mainThreadCheck()
            field = value
            updateExpiry(value) <<< THIS ONE
        }

    ...
}

However, updateExpiry(long) has a behaviour which should be transparent to the clients of SessionWrapper, if they modify expiryTime (i.e. call the setter).

Now, for Kotlin projects, this wouldn't be an issue, since I can just add the extra KDoc to the expiryTime property itself, and it wouldn't feel out of place:

    /**
     * The time in milliseconds after which the session will expire.
     *
     * Updating the expiry time after the session is started does x,
     * the listeners will receive y.
     *
     * Writing comments is fun, when the tools work.
     */
     var expiryTime = DEFAULT_EXPIRY_TIME

But for Java projects, the documentation above would appear for both setExpiryTime(long) and getExpiryTime(), which feels off because I would have setter JavaDoc in the getter, and getter JavaDoc in the setter.

Trying to separate the documentation for the two accessors, in Kotlin, in the following way:

class SomeClass{

    var expiryTime = DEFAULT_EXPIRY_TIME
        /**
         * The time in milliseconds after which the session will expire.
         */
        get() {
            mainThreadCheck()
            return field
        }
        /**
         * Updating the expiry time after the session is started does x,
         * the listeners will receive y.
         *
         * Writing comments is fun, when the tools work.
         */
        set(value) {
            mainThreadCheck()
            field = value
            updateExpiry(value)
        }

    ...
}

just shows no JavaDoc in the IDE, for both Kotlin & Java code.

I found no clear way of trying to separate the docs for Java-visible getters & setters in the KDoc reference or the Java interop page.

I find this pretty annoying, given Kotlin's good interop with Java.

Would appreciate any ideas.

like image 206
cjurjiu Avatar asked Apr 16 '18 08:04

cjurjiu


People also ask

How do you get a getter setter in Kotlin?

There is no "standard getter and setter" in Kotlin. Or, rather, they are built into the language. Your get() and set() are overriding the "standard getter and setter". In Kotlin, fields are already translated into standard getters and setters.

Should you use getters and setters in Kotlin?

In programming, getters are used for getting value of the property. Similarly, setters are used for setting value of the property. In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.

What is accessors in Kotlin?

1.0. interface Accessor<out V> Represents a property accessor, which is a get or set method declared alongside the property. See the Kotlin language documentation for more information.


1 Answers

I think you should reevaluate your class design, instead of trying to explain special behavior in documentation. This is usually a sign of code smell and maybe also a sign of bad testability.

You should model the class with the special behavior of updateExpiry() in mind. If this aspect is worth being transparent to client, it should probably be part of some kind of interface or protocol steps.

Without knowing the details of the rest of your software, the best I can come up with is to just make the setter private and add a separate function for updating expiryTime:

/** Explain property */
var expiryTime = DEFAULT_EXPIRY_TIME
    get() {
        mainThreadCheck()
        return field
    }
    private set(value) {
        mainThreadCheck()
        field = value
    }

/** Explain update behavior constraints */
fun updateExpiryTime(value: Any) {
  expiryTime = value
  updateExpiry(value)
}

IMHO Kotlin's Java interoperability should not be expected to result in code that is just like Java code. It is compatible on the byte code level, not necessarily on the source code and Javadoc level.

like image 155
Brian Avatar answered Oct 11 '22 17:10

Brian