Do Kotlin value types get “packed” when used in ar...
# kotlin-native
k
Do Kotlin value types get “packed” when used in arrays? One of the main goals of Valhalla on the JVM is the following.
Arrays of value types should be packed, without indirections, as arrays of primitives are now.
Since Kotlin has separate, specialized, types for each primitive value how does this bode for value classes on Native? I imagine that
Array<MyValueClass>
would be boxed unless the compiler/runtime were doing some magic I’m unaware of.
k
Ah, so it seems like the answer is not right now, but yes if/when
VArray<T>
lands
This KEEP sections seems pretty JVM specific with not a lot on what native is doing behind the scenes
m
Yes. For now there seems to be little support outside of JVM for it. The whole thing is kinda see what happens with Valhalla and then see how the other platforms get work nicely. Sounds like there are a lot of optimizations they might be able to do on the native side that they just are not doing yet.
k
Brian Goetz comes to a similar conclusion in the proposal for enhanced generics on the JVM that support primitives and value types.
So while
List<String>
and
List<Integer>
will both be represented by the same runtime class
List.class
,
List<int>
will not be (more generally, for all value types
V
and
W
where
V != W
,
List<V>
and
List<W>
will likely be represented by different classes.)
Very similar to
VArray<T>
but more generic. Kotlin/Native could do something similar for all generic types by monomorphizing any value type parameter and doing traditional type erasure for reference types
I raised a similar issue on youtrack a while back through the lens of static dispatch rather than defaulting to virtual dispatch for native
I’m sure the devils are in the details, though 🙂