I am trying to write a library using KMP which nee...
# multiplatform
m
I am trying to write a library using KMP which needs to pass around binary data. If I use ByteArrays they get translated to Int8Array for JS and an exported KotlinByteArray type for Swift (not optimal, but workable). Now since the library needs to perform byte manipulation, I would much prefer to use UByteArrays instead, which unfortunately get exported as any and Any to TS and Swift respectively. Is there a way for me to declare an expect/actual construct that allows me to use UByteArray in the Kotlin implementation and which then gets exported as Uint8Array to JS and [UInt8] to Swift?
a
I can't speak about SwiftExport, but I can add about the TS/JS export things. Right now, all the unsigned arrays for the JS target are compiled into signed arrays. This solution was designed for the JVM target (since JVM doesn't have native unsigned arrays support) and came to other targets as a "legacy," so you can see here that all the unsigned arrays are just
value
classes around signed ones. So, this is the reason why we can't export them as unsigned JS arrays, and the best (at least right now) we can do instead of
any
is to export them as signed arrays (
Int8Array
in your case). Would such a solution improve your experience a bit?
m
The Kotlin-Wrappers library has just recently added corresponding conversion functions. Maybe that can help you.
m
If I understand correctly, this helps with converting the values on the Kotlin side. Is there a way to "fix" the API definitions?
So that consumers can just pass Uint8Arrays for example?