fun calculateNumberOfLengthBytes(data: ByteArray): Int {
val l: Int = data.size
return when {
l < 128 -> 1
l < 16384 -> 2
l < 2097152 -> 3
l < 268435456 -> 4
else -> 5
}
}
but, the following complains about "Incompatible types: Boolean and Int" in the comparisons
Copy code
val data = byteArrayOf(5, 6, 7)
when(val l: Int = data.size) {
l < 128 -> 1
l < 16384 -> 2
l < 2097152 -> 3
l < 268435456 -> 4
else -> 5
}
d
Dico
08/20/2020, 8:58 PM
I'm not sure that you can use
: Int
in this location of a when expression, I don't think I've seen it.
a
alwyn
08/20/2020, 9:07 PM
When I take it out I get the same result. I have only seen the variable from the val being used on the right hand side of the -> in examples. Pity one cant say something like
Copy code
when(data.size) {
< 20 -> 1
...
I won't want to write a parser for that though :)
a
Arkadii Ivanov
08/20/2020, 9:10 PM
Because the latter checks strongly against Int val, it must have ints on the left side.
a
alwyn
08/20/2020, 9:16 PM
ah... the val is implicitly used on the left hand side right? So am I correct that the statement like l < 128 is seen as a Boolean expression and then compared with the val which is of type int?