[reposted] What is the recommended way to use JS a...
# javascript
d
[reposted] What is the recommended way to use JS array from Kotlin to
push()/pop()
items? This seems like a basic thing for JS interop but I did't find any obvious solutions (
kotlin.Array
doesn't have
push()/pop()
methods). The most reasonable solution I found was to define external JsArray class:
Copy code
@JsName("Array")
external class JsArray<T> {
    val length: Int
    fun push(item: T)
    fun pop(): T
    operator fun get(index: Int): T
    operator fun set(index: Int, value: T)
}
(If this is the right thing to do, shouldn't it be in kotlinjs stdlib and mentioned somewhere in docs? 🤔)
k
Things are a bit simpler.
Copy code
inline fun <T> Array<T>.push(e: T): Int = asDynamic().push(e)
s
I guess this is not in stdlib because size of
kotlin.Array
is fixed in java.
d
@konsoletyper thank you 👍 should be in stdlib and in docs
n
Is an ArrayList<T> implemented as an array on the JS platform?
k
No, ArrayList<T> is not JS array, but it includes JS array as a private property, and its 'add` methods just calls
push
on that array