Kotlin arrays are not iterable and primitive arrays are not generic.
This is not possible due to java interoperability.
In effect, some library functions have 9 overloads, one for each primitive type and one for Iterable
(or Collection
). The actual code is often identical.
Without overloads, primitive arrays have to be wrapped with asIterable
before they can be passed as arguments. This causes boxing of items and is significantly slower (something like factor 30).
Would it be possible to allow arrays to be passed instead of iterables under certain circumstances?
Conditions could be
- the method is marked (and used) as inline (this excludes Java calls),
- the iterable parameter is only used in loop expressions and
- the generic bounds of the iterable match the array type
Example:
inline fun contains42(list: Iterable<Int>): Boolean {
for (item in list) if (item == 42) return true
return false
}
// Don't want to have to write this. It's identical to code above.
// inline fun contains42(list: IntArray): Boolean {
// for (item in list) if (item == 42) return true
// return false
// }
val items = IntArray(10000) { Math.random().toRawBits().toInt() }
val current = contains42(items.asIterable()) // boxing is slow
val future = contains42(items) // behave as if calling commented code, much faster