is there some neat way to publicly expose a read-o...
# getting-started
t
is there some neat way to publicly expose a read-only view on a private, mutable collection? i always end up with something like
Copy code
private val mutable = mutableListOf<Something>()
val readonly: List<Something> = mutable
which is kinda ugly
s
k
In your example one could still cast it to
MutableList
and modify it from the outside. To avoid this you should use:
Copy code
mutable.toList()
t
@Sam thanks
@KamilH i'm aware of that and its ok in my case 🙂
👍 1
k
I just checked a KEEP proposal and it states that prototype of this feature has been added to Kotlin 1.7.0. Interesting, because I didn’t notice it in changelog
t
yeah me either. but it looks really cool