Youssef Shoaib [MOD]
04/06/2025, 7:07 PMprivate class TypedMutableMap private constructor(private val map: MutableMap<Key*, Any?>) {
constructor() : this(mutableMapOf())
interface KeyT
@Suppress("UNCHECKED_CAST")
operator fun T get(key: KeyT): T = map.getValue(key) as T
operator fun T set(key: KeyT, value: T) {
map[key] = value
}
fun copy(): TypedMutableMap = TypedMutableMap(map.toMutableMap())
}
This can be made into a value class, I just didn't have the need to do soKlitos Kyriacou
04/07/2025, 8:26 AMMap or MutableMap interface or did you just not have a need for it?Youssef Shoaib [MOD]
04/07/2025, 8:28 AMMap<Key<*>, Any?>, but that's hardly useful. I don't think I can implement MutableMap in any useful wayKlitos Kyriacou
04/07/2025, 8:33 AMMap features, such as values(), which might be useful if you don't care about the exact types but just want a set of values.Chrimaeon
04/07/2025, 6:07 PMget to be typed.
class TypedMutableMap private constructor(
map: MutableMap<Key*, Any?>,
) : MutableMap<TypedMutableMap.Key*, Any?> by map {
constructor() : this(mutableMapOf())
interface KeyT
operator fun T get(key: KeyT): T = this.getValue(key) as T
operator fun T set(
key: KeyT,
value: T,
) {
this.put(key, value)
}
fun copy(): TypedMutableMap = TypedMutableMap(toMutableMap())
}
so technically its still a mutable map.Chrimaeon
04/07/2025, 6:08 PMe: file:///com/cmgapps/compose/Main.kt:44:5 Platform declaration clash: The following declarations have the same JVM signature (get(Lcom/cmgapps/compose/TypedMutableMap$Key;)Ljava/lang/Object;):
fun get(key: TypedMutableMap.Key*): Any? defined in com.cmgapps.compose.TypedMutableMap
fun T get(key: TypedMutableMap.KeyT): T defined in com.cmgapps.compose.TypedMutableMap