I am trying to write my own subclass of LinkedHashMap that executes some additional functionality on update. This looks something like the following:
class MyMap(): LinkedHashMap<String, String>() {
operator fun set(key: String, value: String) {
super.put(key, value)
doSomethingWith(value)
}
}
I then declare a variable of type MutableMap<String, String> and assign an object of type MyMap to it:
val myMap: MutableMap<String, String> = MyMap()
However, if I now try to use my modified operator method
myMap["key"] = "value"
the method I wrote is not called, but the extension function defined for MutableMap in kotlin (kotlin/collections/Maps.kt) is. This part I don't understand, since the declared type of my variable is an interface (so statically resolving the method should not be possible), and the dynamic type is MyMap, which defines the set operator and doesn't have an extension function for it (which should either way not be able to hide an actual implementation in the class itself). If I declare the static type of my variable as MyMap, my function is called as expected. Why does Kotlin resolve the function via the parent class if the declared type of my variable is different?
DISCLAIMER: I am going to solve this problem by overriding the put function, so my question is less about a concrete solution and more about finding an explaination for this imo weird behavior.