简体   繁体   English

Kotlin:属性设置程序的文档

[英]Kotlin: Documentation for property setter

I am writing a Kotlin library. 我正在写Kotlin图书馆。 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 (ie call the setter). 但是,如果updateExpiry(long)修改了expiryTime (即调用setter),则该行为对于SessionWrapper的客户端应该是透明的。

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: 现在,对于Kotlin项目,这将不再是问题,因为我可以将额外的KDoc添加到expiryTime属性本身中,并且不会感到expiryTime

    /**
     * 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 . 但是对于Java项目,上面的文档将同时出现在setExpiryTime(long)getExpiryTime() ,这让人感觉很getExpiryTime() ,因为我将在getter中设置JavaDoc,在setter中设置JavaDoc

Trying to separate the documentation for the two accessors, in Kotlin, in the following way: 尝试通过以下方式在Kotlin中分离两个访问者的文档:

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. 对于Kotlin和Java代码,在IDE中仅不显示JavaDoc。

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 . 我没有找到明确的方法来尝试在KDoc参考Java interop页面中分离Java可见的getter和setter的文档。

I find this pretty annoying, given Kotlin's good interop with Java. 考虑到Kotlin与Java的良好互操作,我觉得这很烦人。

Would appreciate any ideas. 将不胜感激任何想法。

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. 您应该考虑到updateExpiry()的特殊行为来对类进行updateExpiry() 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 : 在不了解其余软件细节的情况下,我能想到的最好的办法就是将setter设为私有,并添加一个单独的函数来更新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. 不应期望恕我直言Kotlin的Java互操作性会导致产生与Java代码相似的代码。 It is compatible on the byte code level, not necessarily on the source code and Javadoc level. 它在字节码级别兼容,而不必在源代码和Javadoc级别兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM