Marshall
10/29/2020, 2:37 PMclass C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element>
get() = _elementList
}
Taken from https://kotlinlang.org/docs/reference/coding-conventions.html#property-namesMarshall
10/29/2020, 2:42 PMclass C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element> get() = _elementList
}Big Chungus
10/29/2020, 2:43 PMclass C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element> get() = _elementList.toList()
}
// This will create a copy of the current state of your mutable listMarshall
10/29/2020, 2:44 PMVampire
10/29/2020, 2:48 PMephemient
10/29/2020, 2:51 PMCollections.unmodifiableList() if you're concerned about it being treated as a MutableListephemient
10/29/2020, 2:52 PMclass UnmodifiableList<T>(private val delegate: List<T>) : List<T> by delegateMarshall
10/29/2020, 2:52 PMNir
10/29/2020, 2:53 PMMarshall
10/29/2020, 2:55 PMNir
10/29/2020, 2:55 PMNir
10/29/2020, 2:55 PMephemient
10/29/2020, 2:55 PMephemient
10/29/2020, 2:55 PMMarshall
10/29/2020, 2:56 PMNir
10/29/2020, 2:56 PMNir
10/29/2020, 2:56 PMMarshall
10/29/2020, 2:56 PMMarshall
10/29/2020, 2:56 PMephemient
10/29/2020, 2:56 PMMarshall
10/29/2020, 2:57 PMNir
10/29/2020, 3:05 PMNir
10/29/2020, 3:05 PMelementList in a loop for example; it's not at all obvious they need to cache it outside the loopMarshall
10/29/2020, 3:06 PMMarshall
10/29/2020, 3:09 PMclass C {
private val _elementList = mutableListOf<Element>()
val elementList: List<Element> = Collections.unmodifiableList(_elementList)
}
Not sure if _elementList changes if elementList reflects those changes, I exepect it wouldMarshall
10/29/2020, 3:11 PMunmodifiableList provides a read only "view" of the underlying listephemient
10/29/2020, 3:23 PMephemient
10/29/2020, 3:23 PMNir
10/29/2020, 3:26 PMvar ImmutableList , using kotlinx.immutableNir
10/29/2020, 3:26 PMval MutableListNir
10/29/2020, 3:27 PMImmutableList inherits from List, so you can just return a List directly in the getter. I don't imagine that people can mutate it on the Java side (maybe I'm wrong). And your class won't mutate it either, just reassign. So it will never mutate the data of the clients of the class either.Nir
10/29/2020, 3:27 PMephemient
10/29/2020, 3:55 PMephemient
10/29/2020, 3:55 PMNir
10/29/2020, 4:04 PMvarNir
10/29/2020, 4:04 PMNir
10/29/2020, 4:05 PMNir
10/29/2020, 4:05 PMMarshall
10/29/2020, 4:07 PMMarshall
10/29/2020, 4:09 PMMarshall
10/29/2020, 4:09 PMMarshall
10/29/2020, 4:11 PMNir
10/29/2020, 4:23 PMNir
10/29/2020, 4:24 PMNir
10/29/2020, 4:24 PMNir
10/29/2020, 4:25 PMNir
10/29/2020, 4:25 PM