I want my class to have a field that is return an immutable list for all public accesses but inside the object instance I want the list to be mutable so I can add elements to it.
class GrammarParser {
private val grammars: List<Grammar> = mutableListOf()
get() = field.toList()
}
Try
class GrammarParser {
private val _grammars: MutableList<Grammar> = mutableListOf()
val grammars: List<Grammar>
get() = this._grammars.toList()
}
I tried this but am unsure if this is considered good practice?