https://kotlinlang.org logo
Title
l

louiscad

12/18/2017, 3:04 PM
This is ugly:
val thisIsAByte: Byte = getValueFromMagic()
when (thisIsAByte) {
    0x00.toByte() -> LOCKED
    0x01.toByte() -> UNLOCKED
    0x02.toByte() -> UNLOCKED_AND_AUTO_RELOCK_DISABLED
    else -> throw IllegalStateException("Unexpected value for lockState: $it")
}
Hope we will soon be able to get rid of the
toByte()
calls
g

gildor

12/18/2017, 3:08 PM
maybe instead convert thisIsAByte to int
thisIsAByte.toInt()
2
l

louiscad

12/18/2017, 4:21 PM
This is a workaround that quadruples the size in memory (ok, not a big deal where it's used), but I ended up using constants instead since I used them elsewhere. Thanks for the suggestion though!
g

gildor

12/18/2017, 4:50 PM
Are you talking about Kotlin/Native or JVM? Not sure about Native, but on JVM
byte
is just
int
in bytecode, and actually even less efficient, because JVM adds value overflow checks for some operations. The only case when byte is more efficient is arrays
Actually, I think even for Native it’s similar, just because CPU register at least 32 bits, so you cannot save memory using
byte
(excluding arrays/buffers)
1