Whats the best way to write unsigned integer liter...
# announcements
f
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
0xFFU.toByte()
?
f
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
There's a
constexpr
ticket for this iirc.
t
“most people think that they are evaluated at runtime, and are thus afraid of them.” - this.
f
even without this misconception I'd say having literals for all types is more consistent and looks nicer
f
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):
i(8|16|32|64|128)
+
u(8|16|32|64|128)
+
f(32|64)
Reminds me that I want to write a KEEP for
Nonzero
now that we have unsigned.