Kotlin:不可变类型对可变类型的内部变量的只读访问

Kotlin: Read Only access of Immutable Type to an Internal Variable of a Mutable Type

在 Android 中学习 ViewModels 时,出现了一个感觉 Kotlin 旨在解决的问题。在下面的代码中,我们可以看到 MutableLiveData 值被用于编辑值和指标。但是,我们不希望这些可变值暴露给其他任何东西,特别是 Android 生命周期的成员。我们确实希望 Android 生命周期成员有权访问读取值但不能设置它们。因此,下面显示的 3 个公开函数属于 LiveData<> 不可变类型。

是否有更简单或更简洁的方法来公开可在内部编辑的只读值?这似乎是 Kotlin 旨在避免的:样板式冗长。

class HomeListViewModel: ViewModel(){
    //Private mutable data
    private val repositories = MutableLiveData<List<Repo>>()
    private val repoLoadError = MutableLiveData<Boolean>()
    private val loading = MutableLiveData<Boolean>()


    //Exposed uneditable LIveData
    fun getRepositories():LiveData<List<Repo>> = repositories
    fun getLoadError(): LiveData<Boolean> = repoLoadError
    fun getLoadingStatuses(): LiveData<Boolean> = loading

    init{...//Do some stuff to MutableLiveData<>

    }
}

可能类似的非Android场景是:

class ImmutableAccessExample{

    private val theThingToBeEditedInternally = mutableListOf<String>()

    fun theThingToBeAccessedPublicly(): List<String> = theThingToBeEditedInternally

    init {
        theThingToBeEditedInternally.add(0, "something")
    }

}

val 没有 setter 因为它是只读的但是如果你想要一个 var 你可以这样做

var repositories = MutableLiveData<List<String>>()
    private set
var repoLoadError = MutableLiveData<Boolean>()
    private set
var loading = MutableLiveData<Boolean>()
    private set

这会给你一个私人 setter 和一个 public getter

我不知道是否可以避免冗长。但是,我以前见过它,它通常被声明为 属性.

private val _repositories = MutableLiveData<List<Repo>>()
val repositories : LiveData<List<Repo>> 
    get() = _repositories

这是惯例,请参阅支持属性的名称

中的the doc here

If a class has two properties which are conceptually the same but one is part of a public API and another is an implementation detail, use an underscore as the prefix for the name of the private property:

我还没有找到解决这个问题的优雅方法,但我就是这样处理的。

private val selectedPositionLiveData = MutableLiveData<Int>()
fun getSelectedPosition() = selectedPositionLiveData as LiveData<Int>

View 通过 public getter 方法进行观察,无需在 ViewModel 中定义第二个成员。我可能更喜欢这种方法,因为我的 Java 背景具有明确的 getters,但在我看来,这与任何其他解决方法一样简洁明了。

遵循this post的想法:

class HomeListViewModel: ViewModel(){
    val repositories: LiveData<List<Repo>> = MutableLiveData()

    init {
        repositories as MutableLiveData
        ...//Do some stuff to repositories
    }
}