. 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
Derek Peirce
11/09/2019, 5:13 AM
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
gildor
11/09/2019, 5:14 AM
Than it wouldn't be possible to distinguish between primitive and boxed arrays in source code, they are incompatible on binary level
h
Hullaballoonatic
11/09/2019, 7:18 AM
Also, why does kotlin throw away the distinction between
int -> Int
and
Integer -> Int
but not
int[] -> IntArray
and
Integer[] -> Array<Int>
?
Hullaballoonatic
11/09/2019, 7:18 AM
if it can throw away the former, why not the latter?
d
Derek Peirce
11/09/2019, 8:37 AM
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
gildor
11/09/2019, 9:31 AM
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