Hey all, new to KMP and exploring the feasibility ...
# javascript
k
Hey all, new to KMP and exploring the feasibility of creating a shared lib that can be used across iOS, Android, and web (TS + React). My specific question is how are people working around the limitation where
kotlin.collections.List
can’t be exported with
@JsExport
? With my limited experience in KMP, see below for something I’ve cobbled together that’s been working but it does not seem ideal. For instance, converting to and from
List
and having to create wrappers for every collection type takes away from the dream of writing business logic once at a high level and reusing it across platforms. Does anyone have advice for doing this in perhaps a more generic way? Has anyone found a way to use
expect / actual
as a better solution?
Copy code
@ExperimentalJsExport
@JsExport
class PeopleList : MutableList<StarWarsPerson> by mutableListOf() {
    fun toArray(): Array<StarWarsPerson> {
        val array = emptyArray<StarWarsPerson>()
        this.forEachIndexed { index, person ->
            array[index] = person
        }
        return array
    }

    companion object {
        fun fromArray(people: Array<StarWarsPerson>): PeopleList {
            val peopleList = PeopleList()
            peopleList.addAll(people)
            return peopleList
        }
    }
}

@ExperimentalJsExport
@JsExport
data class StarWarsPerson(val name: String
b
Create your expect/actual list wrapper that is external interface on js and encapsulates array, while being regular interface encapsulating list on other platforms
Then you can @JsExport js actual
If you need to construct them from common,make some builder functions via expect actual too
k
Thanks for the tips! I’ll give this a shot.
g
See also https://github.com/deezer/KustomExport as a possible alternative to
@JsExport
.
🙏 1
s
FWW, we are shipping a JS/Android/Apple lib and ended up just using
Array
internally and externally. The performance hit you take in JS for using a
List
is staggering. We’re hoping to adopt https://github.com/JakeWharton/platform-collections when it is ready.
thank you color 2