2

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?

xetra11
  • 7,671
  • 14
  • 84
  • 159
  • Question already answered in following link: https://stackoverflow.com/questions/30348292/how-to-create-immutable-list-in-java . Unless you need it in Kotlin syntax? – bramdc Dec 02 '20 at 21:09
  • @bramdc Conversion would not work in the first example since `get() = field.toMutableList()` would show a compiler error since the return value of `grammars` is `List` and not `MutableList` already tried that. Kotlin Syntax should be the way btw. – xetra11 Dec 02 '20 at 21:13

1 Answers1

6

In Kotlin, every MutableList<T> is also a List<T>. So, you can create a MutableList in a private field, and declare an additional field of type List that points to the same object. Inside the class, you can use the mutable reference to modify the list, and expose the List to outside.

Example:

class GrammarParser {
    private val mutableGrammars = mutableListOf<Grammar>()
    val grammars: List<Grammar> = mutableGrammars
}

In your second example, you create a copy (snapshot) of the list every time you call the getter. This means, after you call the getter and got a list, even if the _grammars is modified somewhere else, the changes will not be reflected in the read-only list view you got the reference of. This might be something you want - then what you are doing is ok. But if that's not something you are looking for, then the example I gave above will avoid the copying.

Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49