kotlinx-io
kotlinx-io copied to clipboard
Replace `ByteString(ByteArray)` constructor with `ByteArray.toByteString()`
trafficstars
This current "safe" constructor doesn't feel right. It has the following drawbacks:
- the constructor syntax implies "wrapping" (as if the array is used as a property), but what actually happens is a copy
- it's hard to discover: when I want to turn a
ByteStringinto aByteArray, the completion helps me find thetoByteArray()extension. When I look for the inverse operation, I can't find it via completion - it's not symmetrical with the
ByteString.toByteArray()counterpart - it's not easily composable:
nullableByteString?.toByteArray()is nice, whilenullableByteArray?.let { ByteString(it) }is not nice
Adding a ByteArray.toByteString() extension would solve all of these.