https://kotlinlang.org logo
Title
j

juh juh

08/17/2022, 9:23 PM
Hello, I want to know, which data structure should I use in JS target so it will get compiled into a native JS array without extra overhead? Mainly, I need access to `push`/`pop` and other js-exclusive array methods
j

Jeff Lockhart

08/17/2022, 9:34 PM
kotlin.Array
is mapped to JavaScript
Array
.
j

jw

08/17/2022, 9:51 PM
I usually just write an external interface for a MutableJsArray and then init with
js("[]")
j

juh juh

08/17/2022, 9:52 PM
@jw Thanks you very much! I never knew you can directly run js code, I will go with interface approach too then
g

Grégory Lureau

08/18/2022, 7:28 AM
Why using an external interface instead of just kotlin.Array?
j

juh juh

08/18/2022, 8:22 AM
Because I can't push/pop with it
g

Grégory Lureau

08/18/2022, 8:37 AM
Sorry, I wasn't clear, I was thinking about an extension function on Array that was calling js("this.pop()") for example, but not sure it works.
j

juh juh

08/18/2022, 8:44 AM
Hmm, I will try
Nah, this does not work
fun <T> Array<T>.push(item: T) {
    js("this.push(item)")
}
gets compiled into
function push(_this__u8e3s4, item) {
    this.push(item);
  }
j

jw

08/18/2022, 11:03 AM
Do
val array = this
and then
array.push
inside the JS string