public expect class ZByteArray
public actual typealias ZByteArray = ByteArray
public actual typealias ZByteArray = Uint8Array
The problem with this approach is in common code
ZByteArray
doesn't have any methods or fields, it's useless.
Is there a way to overcome this limitation?
a
Adam S
07/01/2023, 6:30 PM
for any functionality that you need in commonMain you can add expect extension functions, and then delegate to the actual functionality in the actual targets
Copy code
expect fun ZByteArray.foo(): String
you might be better off using Okio though? That’s got good multiplatform support for byte arrays
e
Edoardo Luppi
07/01/2023, 6:32 PM
That's what I was thinking too, about extension functions. I guess it's an ok workaround!
Related to Okio or other libraries, I'm trying to keep dependencies to a minimum, possibly zero.
This helps in deploying this library everywhere I need as it's self contained.
j
jw
07/01/2023, 6:33 PM
You can also switch to value classes which give you "member" functions on the common type
e
Edoardo Luppi
07/01/2023, 6:36 PM
There is a bit of trickery with using that
ZByteArray
tho, like
Copy code
/**
* Reads [n] bytes, and advances the read pointer.
*/
@Suppress("NON_EXPORTABLE_TYPE")
public fun readBytes(n: Int): ZByteArray
You see that I had to suppress a compiler warning.
@jw interesting, could you make an example, I'm not sure how it would be used in this case