https://kotlinlang.org logo
#compiler
Title
# compiler
p

PHondogo

12/21/2021, 10:10 AM
Hello! This code is not compiling:
Copy code
var b : Byte = 10.toByte()
b += 10.toByte()
Complains about type mismatch: Required Byte got Int. Why?
e

ephemient

12/21/2021, 10:12 AM
Copy code
operator fun Byte.plus(other: Byte): Int
e

elizarov

12/21/2021, 10:59 AM
Bytes are designed for storage, so that you can have a lot of bytes in memory. They are not designed for doing arithmetics (that’s what
Int
and
Long
are for). So, do you arithmetics in Ints, then coerce and store to bytes if needed.
🙏 1
p

PHondogo

12/21/2021, 11:16 AM
Thanks!