Everett Corvid
11/15/2023, 7:38 PMJaniruTEC
11/15/2023, 8:36 PMval b: Byte = 0x0F
or
fun doSthWithByte(b: Byte) { TODO() }
fun main() {
doSthWithByte(0x00) //0x00 is a Byte literal here
}
If you are familiar with Java, you might assume 0x0F
to be an int literal, which is implicitly converted.
-- That's not a thing in kotlin. A number literal's type is whatever matches the context. If you want something else, you need to explicitly convert it, e.g. by using toByte
on the number.
Regarding the implementation of toByte
see the docs:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/to-byte.html
The docs of the standard lib also (usually) contain a link to the source (when clicking the prior link, look for "(Common Source)" -- which leads you here:
https://github.com/JetBrains/kotlin/blob/05896a42da04620a2e8ca35e952ff31c3c48aa9d/core/builtins/native/kotlin/Primitives.kt#L1097 -- which in this case is not overly helpful, but I hope you can find your way from there.JaniruTEC
11/15/2023, 8:37 PMEverett Corvid
11/15/2023, 8:59 PMvar x: Byte = 1
if ( x >= 1) print("Hello!")
I understand that this doesn't work as is, and I need to know how it could be possibleJaniruTEC
11/15/2023, 9:00 PMEverett Corvid
11/15/2023, 9:05 PMEverett Corvid
11/15/2023, 9:05 PMEverett Corvid
11/15/2023, 9:09 PMEverett Corvid
11/15/2023, 9:11 PMval test: Byte = -5
if (test >= -4) print("Hello!")
if (test <= -4) print("Hello!")
if (test > -4) print("Hello!")
if (test < -4) print("Hello!")
if (test == -4) print("Hello!")
JaniruTEC
11/15/2023, 9:18 PM==
operator is the same as equals
in Java, ===
does identity comparision (as Java's ==
)
Since Int's ==
operator is only defined for Ints (to keep the general contract of the equals
method), you need to convert the Int to a Byte or the Byte to an Int (size limitations apply)
Since that contract doesn't apply to >=
, >
, <
and <=
they are overloaded to work with all number types.
Because of that, your example works (but only because of one of the helpers):
var x: Byte = 1
if (x >= 1) print("Hello!")
Alternatively you could convert the int to a byte:
var x: Byte = 1
if (x >= 1.toByte()) print("Hello!")
... or convert the byte to an int
var x: Byte = 1
if (x.toInt() >= 1) print("Hello!")
For equality, you have to use explicit conversions.
For further reference, also see: https://kotlinlang.org/docs/numbers.html#explicit-number-conversions
It's explained there far betterEverett Corvid
11/15/2023, 9:23 PMJaniruTEC
11/15/2023, 9:34 PMEverett Corvid
11/15/2023, 10:12 PMRiccardo Lippolis
11/16/2023, 6:19 AMInt.toByte()
, the link to the Kotlin sources provided by Dominik does provide one hint: the method is annotated with @kotlin.internal.IntrinsicConstEvaluation
, which, according to the comments in the source has the following effect:
When applied to a function or property, enables a compiler optimization that evaluates that function or property at compile-time and replaces calls to it with the computed result.
Of course, like mentioned before, due to the fact that Kotlin is multiplatform, it still could depend on the compiler implementation 🙂
Everett Corvid
11/16/2023, 8:45 PMEverett Corvid
11/16/2023, 8:53 PM