Hi. not sure if this is right place but got a ques...
# getting-started
g
Hi. not sure if this is right place but got a question regarding byte in when. I have this small piece of code
Copy code
fun byteInWhen() {
    val event: Byte = 1
    
    when (event) {
        1 -> "one",
        2 -> "two",
        else -> "other"
    }
}
The issue is "Incompatible types: Int and Byte". So how do I write the when statement? I could use .toByte() but seems ugly and wrong
h
I'd go with
when (event.toInt())
. Curious if there's a better way..
👆 1
r
I'm no expert, but my understanding is that numerical literals like
1
only auto-cast to
Int
or larger types, and there's no syntax for direct byte literals (like
f
for floats or
L
for longs). Getting it down to a smaller type like
Byte
just requires a cast/conversion. Or like Huib said you can cast/convert the
Byte
up to an
Int
https://kotlinlang.org/docs/numbers.html
c
if events are limited. would suggest using this way:
Copy code
enum class EventT {
    ONE,
    TWO,
    OTHER
}

fun main() {
    var e = EventT.ONE
    when(e) {
        EventT.ONE -> println("One")
        else -> println("Other")
    }
}
☝️ 1
g
sadly enums will not work here. Im receiving ByteArray from bluetooth device. There is always one Byte in the array
c
take a look at here: