How does HashMap implement the MutableMap interface in Kotlin?

(Note: there is a potential spoiler for one of the Kotlin koans below.)

For a higher order function that takes a function literal as follows:

fun <K, V> buildMap(build: MutableMap<K, V>.() -> Unit): Map<K, V> {
    // How does java.util.HashMap satisfy the MutableMap interface?
    // Does Kotlin support ducktyping?
    val map = HashMap<K, V>()
    map.build()
    return map
}

How is it that java.util.HashMap satisfies the interface MutableMapit is aiming at build? Does Kotlin support any duck style, or is it a special case in Kotlin's language only for certain classes that are part of the JDK?

I looked at the Kotlin documentation for interfaces and searched a bit, but could not find anything that seemed to explain this.

+4
source share
1 answer

Kotlin in Action ArrayList HashMap:

Kotlin , Kotlins MutableList MutableSet, .

, , , , , - Kotlin. , , , , , , , .

, - , .

+2

Source: https://habr.com/ru/post/1671456/


All Articles