How to add abstract closed and open installer?

I have an interface where I want a property that can be changed inside the class, but not outside. I cannot use val because it must be mutable and the var keyword cannot have a specific private installer, as it is in the interface.

In Java, I would do this:

public <T> getMyProperty();

I can use the same approach in kotlin and just write the getter function directly, but this does not look like a kotlin-like approach. Is there a better way to achieve the same as this?

fun getMyProperty()
+5
source share
1 answer

In Kotlin, you can actually overridea valwith var, so I think what you want can be expressed as follows:

interface Iface {
    val foo: Foo
}

class Impl : Iface {
     override var foo: Foo
         get() = TODO()
         private set(value) { TODO() } 
}

val :

class ImplDefaultGetter : Iface {
    override var foo: Foo = someFoo
        private set
}

.

+11

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


All Articles