How do folks working from JavaScript interacting w...
# javascript
b
How do folks working from JavaScript interacting with Kotlin/JS libraries deal with scenarios like this? Seems there is a fair amount of mangling that happens that makes it very difficult to interact with Kotlin collections from JavaScript
e
Is it an option to change the Kotlin code to take an Array instead of a List?
a
I usually try to use platform-specific declarations to create functions specifically for JS that accept Arrays. call .toList() on them, and pass them through to my regular List-friendly code
b
Okay so sounds like people generally just work around this? We could change the code, but we ran into this after already having a lot of code written and then deciding we wanted to transpile to JS. List is a reasonable choice for a lot of public APIs, which is how we ended up here :)
a
Yeah it's definitely a pain and Lists definitely make sense for many public APIs. I don't rewrite public APIs to force Arrays for all platforms though. I instead create a wrapper in jsMain that redeclares the public API and transforms between Arrays/Lists just to make JS interop easier.
Another place you might run into this is kotlin Longs vs JS numbers 🙂
b
👍 It’d be great if there were tooling to do this. I might look into it. Need to be able to use the AST to properly codegen wrappers though
👍 1
Open to other ideas on how to codegen this (wrappers) if others are more familiar with the tooling!
s
Hey @basher, I’m not sure if you’ve seen any of the work on #arrow-meta but since it uses the IR back-end something like this should be possible for KotlinJS. In some areas where the Kotlin compiler doesn’t support IR yet, such as
suspend
, you cannot build MPP compiler plugins yet but for something like this it should definitely be possible.
You could also write an annotation or just generate all the
Array
functions over the
List
functions
b
Ooh neat! Not sure I have time at the moment actually, but I ended up filing this ticket for now: https://youtrack.jetbrains.com/issue/KT-34995 We could definitely do some work to make our APIs more JS friendly, but at the same time, it feels like there is some room from improvement from Kotlin/JS as well.
Will definitely checkout arrow-meta for these things in the future though! 🙂
👍 1