Hello, I have such method in a library: ```open fu...
# announcements
n
Hello, I have such method in a library:
Copy code
open fun track(event: String, properties: Map<Any?, *>?, options: Map<Any?, *>?): Unit
How can I create a map that would have satisfy
properties
type?
s
Copy code
fun track(event: String, properties: Map<Any?, *>?, options: Map<Any?, *>?): Unit {
    // implementation    
}

track("", mapOf("test" to 1, 1 to 2, null to null), mapOf("a" to ""))
You can put every type you want in this 🤔
n
Hmm, indeed
But when I try this
Copy code
val p = mutableMapOf<String, Any>()
track("", p, mapOf("a" to ""))
it fails with Type mismatch: inferred type is MutableMap<String, Any> but Map<Any?, *>? was expected
How can I overcome it?
s
Declare
p
type
Copy code
val p: Map<Any?, String> = mutableMapOf...
n
Didn't quite help 😞
Copy code
val p: Map<String, Any?> = mutableMapOf<String, Any>()
track("", p, mapOf("a" to ""))
Type mismatch: inferred type is Map<String, Any?> but Map<Any?, *>? was expected
s
why are you parameterizing the call to
mutableMapOf()
with
String, Any
if you need
Any?, *
? keys in maps are invariant — just call
mutableMapOf<Any?, Any>()
and put your entries in
n
I totally missed it out. Thanks!