In Kotlin, how to make a property accessible by only specific type

Viewed 1079

Lets say I have a Kotlin class similar to this:

class MyKotlinExample {
    val mMyString = MutableLiveData<String>()
}

MutableLiveData extends LiveData however I don't want to expose MutableLiveData to other classes. They should only see/access LiveData<String> as my special String

Is it possible, and/or good/advised etc?

5 Answers

Kotlin creators heard our voice and provided a simple way of doing this on Kotlin 1.7 :

private val item = MutableLiveData<Item>()
    public get(): LiveData<Item>

or just:

val item = MutableLiveData <Item>()
    get(): LiveData <Item>

Note: It is currently subject to opt-in and have limited support, so we will have to wait for a bit longer for stable release.

Related