https://kotlinlang.org logo
Title
k

Konstantin Petrukhnov

08/10/2018, 12:53 PM
MutableMap.toMap() return mutableMap if size >1. How I should get immutable map from mutable map?
🤔 1
a

adam-mcneilly

08/10/2018, 1:00 PM
Can you post the code you're using? I tried this, and I get an error that it cannot resolve `put`:
val mutableMap = mutableMapOf(1 to 1, 2 to 2)
val immutableMap = mutableMap.toMap()
immutableMap.put(3, 3)
a

Andreas Sinz

08/10/2018, 1:01 PM
There are no immutable collections in stdlib, just read-only views to mutable collections
👍 1
k

Konstantin Petrukhnov

08/10/2018, 1:22 PM
@adam-mcneilly I'm not trying to modify it. But I got error: instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift. Other exceptions can be propagated as NSError if method has or inherits @Throws annotation. Uncaught Kotlin exception: konan.worker.InvalidMutabilityException: mutation attempt of frozen {a=[E,S,D], b=[C,F,G]} (hash is 0x45f9e1aa) at 3 0x00000001027ec278 kfun:kotlin.collections.HashMap.<get-entries>()kotlin.collections.MutableSet<kotlin.collections.MutableMap.MutableEntry<#GENERIC,#GENERIC>> + 1172 at 4 0x000000010278817c kfun:a.b.c.MyService.getStringValue$M(kotlin.Long)kotlin.String? + 424 And it point to line, where I'm trying to get value from map.
And from kotlin sources: /** * Returns a new read-only map containing all key-value pairs from the original map. * * The returned map preserves the entry iteration order of the original map. */ @SinceKotlin("1.1") public fun <K, V> Map<out K, V>.toMap(): Map<K, V> = when (size) { 0 -> emptyMap() 1 -> toSingletonMap() else -> toMutableMap() }
a

Andreas Sinz

08/10/2018, 3:17 PM
thats something for #kotlin-native