Reputation: 15336
Kotlin documentation for groupBy
shows special form for every type, like ByteArray
, IntArray
. Why it is so, why the single generic form is not enough?
inline fun <T, K> Array<out T>.groupBy(
keySelector: (T) -> K
): Map<K, List<T>>
The snippet from Kotlin documentation
inline fun <T, K> Array<out T>.groupBy(
keySelector: (T) -> K
): Map<K, List<T>>
inline fun <K> ByteArray.groupBy(
keySelector: (Byte) -> K
): Map<K, List<Byte>>
inline fun <K> ShortArray.groupBy(
keySelector: (Short) -> K
): Map<K, List<Short>>
inline fun <K> IntArray.groupBy(
keySelector: (Int) -> K
): Map<K, List<Int>>
...
Question 2
It seems like IntArray
is not subclassing the Array
and that's probably the reason why it is necessary.
So, I wonder - if I would like to add my own function, let's say verySpecialGroupBy
- does it means that I would also need to specify not just one such function - but repeat it for every array type?
Or it's a very specific and rare case when you would need to use those special arrays and in practice you can just define your function for generic Array
and ignore the rest?
Upvotes: 2
Views: 70
Reputation: 2668
Those array specializations are for arrays of primitive types. So in your example of creating a verySpecialGroupBy
function, you would only need to repeat it for each specialization if you wanted to use it with primitive type arrays.
You can read more about the need of primitive type array in this Kotlin discussion thread.
Upvotes: 1