2

Kotlin somehow considers java.util.Collection as extending kotlin.collections.MutableCollection.

Is there any way for this same functionality to be used by code written outside the Kotlin standard libraries?

e.g., I want to create my own Kotlin NavigableMap & MutableNavigableMap interfaces (with the latter extending the former). I want java.util.NavigableMap considered as extending my MutableNavigableMap.

XDR
  • 4,070
  • 3
  • 30
  • 54
  • Both kotlin.collections.MutableCollection and kotlin.collections.Collection are compiled (on the JVM) to java.util.Collection. There is no 'subtyping.' – lukas.j Dec 24 '21 at 13:47
  • A `java.util.Collection` instance can be assigned as the value of a `kotlin.collections.Collection` variable, but not vice versa. Kotlin behaves as if the former is a subtype of the latter, despite the java code for the former not having anything to do with Kotlin. Can I do the same for types that I designate? – XDR Dec 24 '21 at 13:59
  • I don’t see anything disparaging unless a comment was deleted. I’m pretty sure you cannot get the behavior you’re hoping for without creating your own fork of the Kotlin compiler. – Tenfour04 Dec 24 '21 at 14:09
  • The first respondent incorrectly stated that "There is no 'subtyping'.". That disparages my question as invalid. But they were wrong; Kotlin inserts types in the Kotlin type hierarchy for non-Kotlin code. – XDR Dec 24 '21 at 14:29
  • @XDR: you are wrong, and of course you can assign a variable conforming to kotlin.collections.Collection to a variable conforming to java.util.Collection. You need to do a cast, for example like this: _val stringCollection: java.util.Collection = ArrayList() as java.util.Collection_ – lukas.j Dec 24 '21 at 14:50
  • @XDR: I was wondering, did you just want to know if this was possible to do in Kotlin or do you have a use-case where it would be too cumbersome to do it in another way, such as defining your interfaces and using an adapter pattern to convert from/to java.util.NavigableMap when interfacing with 3rd party code? – Ma3x Dec 24 '21 at 14:57
  • @XDR: Wrong. Kotlin's Collection interfaces exist purely for the IDE for syntax checking. All Collection code is directly mapped to Java classes. – lukas.j Dec 24 '21 at 15:03
  • @Ma3x Why would I write adapter code if Kotlin provides this functionality to me? I imagine Kotlin doesn’t provide this functionally to code outside the Kotlin core libraries, but I’m asking to find out. – XDR Dec 24 '21 at 15:04
  • @XDR: Yeah that is what I imagined. So you were just asking if it was possible, but since it is not, you will define your own interfaces (and adapt java.util.NavigableMap, if it will be needed). If all the related code is under your control then an adapter wont be needed. I was just assuming that maybe you were writing a library that would have to interface with java.util.NavigableMap / SortedMap a lot. – Ma3x Dec 24 '21 at 15:10
  • @Ma3x I want an interface for NavigableSet that only includes non-mutating functions / methods. The Java one includes the mutating methods. I want a separate MutableNavigableSet interface that includes all the functions. – XDR Dec 24 '21 at 15:19
  • @lukas.j You are wrong yet again. It is possible to create a subtype of `kotlin.collections.Collection` that is not a `java.util.Collection`. The API of the former is a subset of the latter. They are not the same thing. – XDR Dec 24 '21 at 19:45

0 Answers0