Auto-boxing is a known performance concern on Android which caused many people to implement multiple versions of classes (1 for each primitive type).
I would like to propose the ability to define primitive generics:
class MyList<primitive T> {
private var values: Array<primitive T>() // primitive array of the same type
fun add(value: T) {
....
}
}
fun test() {
val optimizedList = MyList<primitive Int>() // primitive Int version of MyList
}
So with this proposal, the Kotlin compiler would auto-generate the primitive-specific version of that class depending on which primitive is used.
In addition to avoiding auto-boxing / unboxing, this would also eliminate the need for the implicit casts that the compiler adds. Another benefit would be that we no longer experience type erasure with these classes for the class-level generic type (eg. similar to reified types). Lastly, this would also enable more cache-friendly collections since the data would be stored in a contiguous chunk rather than chasing references.
Would it be possible to get something like this in a future Kotlin release?