https://kotlinlang.org logo
Title
a

Andreas Sinz

03/13/2018, 11:45 AM
@nickk you could create your own List-Type and use delegation
class MyImmutableList<T>(private val list: List<T>): List<T> by list
then the user won't be able to cast your list into a
MutableList
and therefore won't be able to edit it
a

araqnid

03/13/2018, 1:28 PM
isn't that just a reinvention of Collections.unmodifiableList ?
(which would also do things like protecting against calling remove() on the iterator, for example)
a

Andreas Sinz

03/13/2018, 1:42 PM
they are pretty similar, my example prohibits the cast to
MutableList<T>
,
Collections.unmodifiableList
just throws an exception when you call
add