在Map.minus(Map)结果键上键入擦除

时间:2019-06-23 21:41:08

标签: android-studio generics kotlin hashmap type-erasure

示例代码:

val map1: Map<String, DataClass> = ...
val map2: Map<String, DataClass> = ...
val result = map1 - map2

result的类型在Android Studio中表示为Map<Any, DataClass>,迫使我诉诸于:

val result = (map1 - map2).mapKeys { it.key.toString() }

文档使我期望将K类型保留为String,还是我在这里遗漏了什么?

/**
 * Returns a map containing all entries of the original map except those entries
 * the keys of which are contained in the given [keys] collection.
 *
 * The returned map preserves the entry iteration order of the original map.
 */
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V> =
    this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()

1 个答案:

答案 0 :(得分:0)

我的错;我显然没有足够地阅读文档。

修复:

val result = map1 - map2.keys