(Note: There is a potential spoiler for one of the Kotlin Koans below.)
Given a higher order function that takes a function literal like this:
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 MutableMap
interface that build
is targeted against? Does Kotlin support some sort of ducktyping or is this a special-case in the Kotlin language just for certain classes that are part of the JDK?
I looked at the Kotlin documentation on interfaces and searched a bit but couldn't find anything that seemed to explain this.