https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thana

07/11/2022, 8:45 AM
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

Sam

07/11/2022, 8:51 AM
k

KamilH

07/11/2022, 8:51 AM
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

thana

07/11/2022, 8:54 AM
@Sam thanks
@KamilH i'm aware of that and its ok in my case 🙂
👍 1
k

KamilH

07/11/2022, 8:55 AM
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

thana

07/11/2022, 9:03 AM
yeah me either. but it looks really cool
8 Views