11/10/2023 0 Comments Kotlin immutable list![]() ![]() ![]() However due to Kotlin's delegation feature the implementations become one liners: class ImmutableList(private val inner:List) : List by innerĬlass ImmutableMap(private val inner: Map) : Map by inner I couldn't find a copy constructor in the collection factory methods mutableMapOf() or mutableListOf() and so I was wondering what is the best and most efficient way to turn a mutable collection into an immutable one.Ĭurrently in Kotlin stdlib there are no implementations of List ( Map) that would not also implement MutableList ( MutableMap). Which means an API user could simply cast my returned map as an ImmutableMap and gain access to the precious private data internal to my class, which was intended to be protected of this kind of access. I managed to expose only Map and List (which are immutable) in the public API of my class, but the instances I am actually exposing are still inherently mutable. I wanted to expose my data to the API user, but to avoid any unsafe publication of my data I wanted to expose it in immutable collections even when internally being handled by mutable ones. I was writing a small piece of code in which I internally handle my data in a mutable map, which in turn has mutable lists.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |