Hi, using Kotlin on Android since last October, I'...
# language-proposals
l
Hi, using Kotlin on Android since last October, I've been developing apps interacting through Bluetooth and Bluetooth Low Energy. That's where you deal a lot with bytes, and hardcoding bytearrays has been really painful. You need to add
0x
prefix to each hex byte, and when it reaches values that are equivalent negative 8 bit integers, you need to add
.toByte()
, plus the comma and space between each byte. This is extremely verbose, and I had to use hex string to byte array conversion instead to make my code readable. Could Kotlin 1.2 allow us to write byte array literals more easily, please? Here's how I can do it for now:
Copy code
private val SERVICE_DATA_MASK = byteArrayOf(0xFF.toByte(), 0xFF.toByte(),
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
)
Here's how I'd like to do it:
Copy code
private val SERVICE_DATA_MASK = [0xFF_FF_00_00_00_00_00_00_00_00_00_00_00_00]
I'd also enjoy being able to omit the first hex character of a byte if it's zero to shorten it further:
Copy code
private val SERVICE_DATA_MASK = [0xFF_FF_0_0_0_0_0_0_0_0_0_0_0_0]
What do you think about this? Anyone has a better syntax proposal?