Whats the best way to write unsigned integer literals for signed types? For example I'd like to write
0xFF
and have that be converted to a signed
Byte
d
Dominaezzz
04/24/2021, 9:40 AM
0xFFU.toByte()
?
f
Fleshgrinder
04/24/2021, 10:34 AM
At some point we really should clean this API up.
Copy code
fun inconsistent() {
val byte = 1.toByte()
val ubyte = 1.toUByte()
val short = 1.toShort()
val ushort = 1.toUShort()
val int = 1
val uint = 1.toUInt()
val long = 1L
val ulong = 1.toULong()
val float = 1F
val alsoFloat = 1f
val double = 1.0
}
fun consistent() {
val byte = 1b
val ubyte = 1ub
val short = 1s
val ushort = 1us
val int = 1i
val uint = 1ui
val long = 1l
val ulong = 1ul
val float = 1f
val double = 1d
}
Sure, one can use the conversion functions all the time, but most people think that they are evaluated at runtime, and are thus afraid of them.
d
Dominaezzz
04/24/2021, 10:35 AM
There's a
constexpr
ticket for this iirc.
t
Tim Putnam
04/24/2021, 12:07 PM
“most people think that they are evaluated at runtime, and are thus afraid of them.” - this.
f
F0X
04/24/2021, 6:24 PM
even without this misconception I'd say having literals for all types is more consistent and looks nicer
f
Fleshgrinder
04/24/2021, 6:51 PM
No argument there, on the contrary, I think that this byte, short, int, long, float, and double naming in itself creates inconsistency. The way Rust handles this is consistent and much more logical (at least to me):