Is there a reason `kotlin.Array<E>` doesn't ...
# stdlib
r
Is there a reason
kotlin.Array<E>
doesn't implement
kotlin.collections.Iterable<E>
? They both define
public operator fun iterator(): Iterator<E>
. I know Java arrays don't, but does that limit Kotlin ones? (I've often thought it would be convenient if
Array<E> : FixedLengthMutableList<E>
&
FixedLengthMutableList<E> : List<E>
, if that could all be done synthetically at compile time - is it reflection that prevents this? If so another reason to loathe reflection...)
I note Scala doesn't do anything like this either, and they're generally more brutal about favouring types over compatibility, so I guess there are fundamental reasons for not making
Array
implement a collections interface. Be interested to know what they are.
d
On JVM
kotlin.Array<E>
is replaced by javas
E[]
, which can not be somehow modified by Kotlin, so Kotlin inherit all contracts and limitations for arrays from Java
🙏 1
r
Yes, I was wondering if something synthetic could be done at compile time, but I guess to maintain interoperability with Java if you allowed an
Array
to be typed as an
Iterable
you'd have to do a runtime check every time you passed an
Iterable
to a non-Kotlin method in case it were an
Array
, in which case it would need to be boxed using
Arrays.listOf
.
d
But why do you need this? It won't bring much value but may lead to a lot of implicit overhead when array will be converted to list Basically, arrays are needed for two things: 1. Reading raw input (
ByteArray
and IO) 2. Low-level implementation for actual collections For everything else, there's Mastercard
array.toList()
r
I don't need it, it's just a (very) mild itch, so I was curious what the reason was - I mean, you don't "need" a common supertype of
Set
&
List
, you could just use
toSet
and
toList
whenever you need to convert between them, but when all you want to do is iterate over them it's nice not to have to do that. There are loads of Java APIs that are still array based.