lundi 22 juin 2020

Kotlin: Assert Immutability

I have class that internally maintains a mutable list, and I want to provide an immutable view on this list. Currently I'm using the following:

/**The list that actually stores which element is at which position*/
private val list: MutableList<T> = ArrayList()

/**Immutable view of [list] to the outside.*/
val listView: List<T> get() = list.toList()

First question: Can this be done easier

Second question: How can I test that listView is actually immutable. I guess reflections are necessary?





Aucun commentaire:

Enregistrer un commentaire