Annotate getter from property

In kotlin we can define

var text: String

which will have a getter and setter. It's easy to remove the installer by changing it to val, but what if we have a property that should only have a setter and no getter? Is there a way to create an annotation that we can apply to textthat will invalidate the getter so that others cannot use it?

The ultimate goal is to use property syntax rather than calling setText

The current workaround I know achieves a similar result:

var text: String
    @Deprecated(level = DeprecationLevel.ERROR, message = "Non readable property")
    get() = nonReadable()
    set() ...

I would like to know if it is possible to define something similar to

@SetterOnly
var text: String
    set() ...
+4
source share
1 answer

, kotlin. KT-3110 Fixed.

java kotlin , setter setText:

public class Property {
   public void setText(String text) {/**/}
   private String getText() { return "foo"; }
}

, / . getter kotlin-allopen. , , kapt, , :

@SetOnly var text:String = "foo";
+1

Source: https://habr.com/ru/post/1680054/


All Articles