What is the point of `ByteArray`, `IntArray`, `Cha...
# announcements
h
What is the point of
ByteArray
,
IntArray
,
CharArray
etc? why not just
Array<Byte>
,
Array<Int>
,
Array<Char>
. I know that Java does treat primitive arrays differently from object arrays, but couldn't Kotlin do us a solid and smoosh them all into just
Array<T>
?
d
That gets problematic when
Array<Int>
sometimes means
Integer[]
(when it's
Array<T>
and learns of its type at runtime) and other times means
int[]
.
g
Than it wouldn't be possible to distinguish between primitive and boxed arrays in source code, they are incompatible on binary level
h
Also, why does kotlin throw away the distinction between
int -> Int
and
Integer -> Int
but not
int[] -> IntArray
and
Integer[] -> Array<Int>
?
if it can throw away the former, why not the latter?
d
Int
refers to
int
except when generics or nullability are involved, in which case it becomes
Integer
. Converting between the two is trivial (relatively), so there's no issue. However, converting between
int[]
and
Integer[]
can be prohibitively expensive.
g
Also from Java it's not a problem to pass int to Integer or vice versa,, but primitive array are not interchangeable with Object arrays of corresponding types. One more reason for this, that you just cannot write a function that doesn't involve boxing for generic array, which essentially makes primitive arrays useless, if you need primitive array, you don't want to have boxing Think about primitive arrays as low level abstraction that useful in performance critical cases and provides efficient low level access to memory, but shouldn't be used in most other