The issue seems to be with the first ‘Any’ in the Map
I get the following compilation error:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val Map<Any, List<Any>>.listElementCount: Int defined in root package in file TestExtensionFunction.kt public val Map<Int, List<Any>>.listElementCount: Int defined in root package in file TestExtensionFunction.kt
class TestExtensionFunction {
val myMapDouble = mutableMapOf<Int, List<Double>>()
val myMapString = mutableMapOf<Int, List<String>>()
val myMapStringString = mutableMapOf<String, List<String>>()
init {
myMapDouble[1] = listOf(2.0,2.3)
myMapDouble[2] = listOf(5.0,5.1)
myMapString[1] = listOf("my", "first", "entry")
myMapString[2] = listOf("my", "second", "entry")
myMapStringString["1"] = listOf("my", "first", "entry")
myMapStringString["2"] = listOf("my", "second", "entry")
println("myMapDouble contains ${myMapDouble.listElementCount} elements")
println("myMapString contains ${myMapString.listElementCount} elements")
println("myMapString2 contains ${myMapStringString.listElementCount} elements") // does not compile
}
}
val Map<Int, Collection<Any>>.listElementCount: Int
get() {
var total = 0
for (list in this.values) { total += list.size}
return total
}
val Map<Any, List<Any>>.listElementCount: Int
get() {
var total = 0
for (list in this.values) { total += list.size}
return total
}
Update: I found out it works when i define it with ‘out Any’, but I have no clue why it would require out in this case:
val Map<out Any, Collection<Any>>.listElementCount: Int
get() {
var total = 0
for (list in this.values) { total += list.size}
return total
}