Is there a fundamental difference between `IntArra...
# announcements
b
Is there a fundamental difference between
IntArray
and
Array<Int>
? If so, is there a reason why the compiler doesn’t treat them as type aliases?
d
Yes, there is.
Array<Int>
is an array of boxed values (like
java.lang.Integer
).
IntArray
is an optimized array of primitive values.
r
Perhaps more precisely
IntArray
is
int[]
whereas
Array<Int>
is
Integer[]
.
d
Yes, I was trying to explain it without being specific to Java.
On JS
IntArray
is using typed arrays,
Array<Int>
is just a normal javascript array, as far as I know.
🤔 1
r
Good point, I often overlook the JS and native applications of Kotlin.
b
JS has actual typed arrays? huh
Doesn’t the compiler change
Int
to
int
and
Int?
to
Integer
when the type is alone? So it seems odd to me that
Array<Int>
isn’t converted to
int[]
but still have
Array<Int?>
go to
Integer[]
d
The problem is that
Array<Int>
needs to be assignable to things like
Array<out Any>
.
If
Array<Int>
compiled to
int[]
, every array would have to be represented as
Object
on the JVM and you would have to use
java.reflect.Array
for all accesses.
b
Ahh, variance is a good reason — it’s different having to consider that primitives are more relevant in Java than they are in Kotlin