Hi there :slightly_smiling_face: why does Kotlin ...
# getting-started
h
Hi there 🙂 why does Kotlin need
Array
in the first place? Why not use
Collection
and let the compiler optimize them away as JVM Arrays where possible?
v
Arrays are here because Java has them, and thus K has to express this concept.
You are correct, it is strongly not recommended to use arrays unless you know what you are doing and you need that extra little bit of performance.
Instead one should use lists in the code, which in most cases are
ArrayList
h
not sure I got what you said, sorry. The concept of Arrays could be expressed by a K Collection and the K compiler could provide the necessary magic. And
ArrayList
is a class, not a K class, so I'm not sure why that would be relevant.
d
You can't add interfaces to object by compiler magic. You can't make Java's array objects implement
Collection
by compiler magic.
v
Now I am not sure what it is we are talking about.
Collection
is an interface,
Array
is a class, one can't replace the other.
There is no real distinction between kotlin classes and java classes. I mentioned
ArrayList
as an example of a
List
which is indeed very optimized by JVM
h
what I meant is getting rid of
Array
altogether and only provide the
Collection
type hierarchy - an Array being essentially a mutable, fixed-sized List. I have yet to understand your "Arrays are here because Java has them, and thus K has to express this concept." I'm not saying you are wrong, but I still don't really understand the "why". 🙂
d
Arrays are the only real way to create a data structure on the JVM (linked lists aside). Everything (HashMap, ArrayList, etc) is ultimately based on them. If Kotlin does not provide means to access this mechanic, you cannot write any data structures in Kotlin.
h
yeah, but ArrayList for instance does already all the array access, and kotlin just uses it. Why would kotlin have to have his own
Array
type?
d
Because
ArrayList
is a high-level implementation. You need low-level building blocks (like arrays) for when you need performance or exact control.
For example, something like
AtomicReferenceArray
is not possible if you don't have arrays.
h
Yes, I understand and I don't doubt that the JVM needs arrays. I don't understand why kotlin has to expose them. Performance is largely a compiler optimization thing, no?
and
Array
is only one-dimensional, so it doesn't even "express the complete concept" of JVM/Java Arrays.
d
JVM arrays are one-dimensional, too. "Multi-dimensional" arrays are just arrays of pointers to other arrays.
Kotlin arrays work the same way:
Array<Array<String>>
works fine.
h
I see, thanks.
d
It's not just about performance. You can do things with arrays (e.g. do CAS on the elements, at least when we get varhandles in java 9, before that you need Unsafe) that you cannot do with ArrayList.
h
CAS?
d
compare and swap, an operation you need for high-efficiency concurrent algorithms
👍 1
v
Arrays are here because Java has them, and thus K has to express this concept.
Consider if java exposes overloads
test(String[] x)
and
test(List<String> y)
, how do you call one over the other if you can't provide an array?
👍 1
👍🏼 1