https://kotlinlang.org logo
Title
m

mzgreen

06/11/2019, 1:40 PM
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:
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

Ruckus

06/11/2019, 1:57 PM
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.
val foo: UByte = 0x1u

when (foo.toUInt()) {
    0x1u -> ...
}
m

mzgreen

06/11/2019, 2:06 PM
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

Ruckus

06/11/2019, 2:12 PM
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