Exposing a Mutable member as Immutable

This is how I do it:

// public type of the property is List, i.e. not mutable:
val foos: List<String> = mutableListOf()

fun internalModifier() {
    foos as MutableList
    // from here on we can use foos as a MutableList

    foos.add("foo")
    foos.add("bar")
}
1 Like