Hey guys ! I'm working on kotlin multiplatform wit...
# javascript
n
Hey guys ! I'm working on kotlin multiplatform with IR js compiler and I wonder some few things about the ability of using kotlin collections in js. I have created a stack-overflow post about it : https://stackoverflow.com/questions/70437501/kotlin-multiplatform-ir-js-how-to-simply-use-kotlinx-collections-from-js-m Hope someone know a bit about this issue ! (We can talk about it here (not necessarily on stack))
a
My opinion is that you should steer clear of calling the Kotlin collections from JS. The JS collections are pretty well optimized by the respective JS runtimes and I'd propose to just use the Array<T> equivalent in any JS-facing code. My question to you is: Why would you want this functionality?
n
Simply cause I have a kotlin-multiplatform js library that I use from both my kotlin server and my client js (it's a shared logic library) and as this library manipulate some kotlin collections I need to manipulate them in the js code too, thus when I need to construct "array" I need to construct the kotlin-js equivalent ArrayList/MutableList
a
Yes, I understand what you mean. We have the same issues in our application, which is also a Kotlin server and a Kotlin/JS library in a vanillaJS webapp. The Array class is the KotlinJS equivalent. The other ones will always wrap the JS array I think. How we solve it is just create constructors for our classes where lists are required as arguments with arrays instead and just expose those constructors to the client JS. Or have
fromArray
methods as extensions.
r
IIRC just using a Kotlin
Array
will compile to plain JS arrays, though the editor states that has a negative effect on comparisons when using it in data classes. You might then want to introduce a boundary objects which expose only
Array
rather than `ArrayList`/`MutableList`