Error with number literal

Hi

using generic Array [ Byte ] leads to

error: the integer literal does not conform to the expected type Byte

when assigning n = 255 etc…
i think the overhead 255.toByte() is too expensive
also would be great to support unsigned as well

First of all, you may want to use a ByteArray rather than an Array<Byte> as the former is an array of primitives, the latter an array of boxed Byte objects. Second, while toByte is clunky to write it does not actually represent any bytecode overhead. Remember that the constant pool doesn’t have bytes. Neither do most operations work on bytes, they work on Int’s that are then truncated/converted when stored in an array. In other words, even in Java there is additional code involved to work with bytes as mathematical operations are only defined on int, long, float and double types (single or double word types)

1 Like

Also unsigned types for Kotlin are almost certainly on their way though not in the forthcoming version 1.2.

Bytes are signed, so they range from -128 to 127.
They must be “cast” from Ints, but they can be assigned from Int literals within range.

val b0 = 255.toByte()
val b1: Byte = -1

assert(b0 == b1)

It’s just too bad you can’t use 0xff (as far as I know).