Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using plus equals operator with bytes

Tags:

kotlin

The code below gives me the following error.

Error: Type mismatch: inferred type is kotlin.Int but kotlin.Byte was expected

var temp: Byte = 0
var temp2: Byte = 1
temp += temp2

Is there any way around this in kotlin or am I not allowed to use the += or -= operators with Byte? Is plus equals overloaded for Long and Int but not Byte and Short?

like image 668
frognosis Avatar asked Oct 23 '25 14:10

frognosis


1 Answers

According to kotlin docs Byte's plus/minus operations with other Byte will result in an Int. So while you think it is weird try to add Byte with value of 255 to other Byte with calue of 255 ;)

I think they made it on purpose. If you are certain that your result is still within Byte bounds then just use Int.toByte() and the end of the calculations.

like image 122
Rafal G. Avatar answered Oct 26 '25 16:10

Rafal G.