Second question - if I write `val foo = 0x1u` it's...
# announcements
m
Second question - if I write
val foo = 0x1u
it's treated as
Int
. Is there a way to make it something else without providing a type? For example:
Copy code
val foo: UByte = 0x1u

when(foo) {
    0x1u -> {} // doesn't work because 0x1u is treated as an Int
}
I can do
0x1.toUByte()
but it's ugly. Is there maybe something like
0x1ub
suffix or sth like that?
r
No, there can't be. One downside of type inference (in any language) is that primitive (or potentially any "built in") values can be ambiguous, so a default has to be chosen. For integral types, it's
Int
(or
UInt
with
u
suffix). it's the same reason
val x = 5
doesn't create a
Byte
unless you specify.
Instead of using
.toUBute()
on the literal, you could try using
.toUIint()
on foo to see if you like that more.
Copy code
val foo: UByte = 0x1u

when (foo.toUInt()) {
    0x1u -> ...
}
m
Makes sense, not sure if adding unsigned types to Kotlin was worth the effort. It's still a pain to code low level stuff that operates on bytes and bits.
r
You should realize that adding support for unsigned types wasn't the end goal, just the first step in a long (and ongoing) story. There are a number of ongoing discussions about the best way to implement operators for the various primitive types, as well as improving the whole bit fiddling experience (and lots of other low level type stuff). Hopefully it will get MUCH better with time as more of the story plays out.
­čĹŹ 1