Guys, the IDE asks me to convert the comparison of...
# getting-started
n
Guys, the IDE asks me to convert the comparison of `UByte`s into a range check. And I would gladly do it, but if I do
if (f.value in from..to && !c.isEmpty)
, then it doesn’t compile saying
Type interference failed
. Any other options?
Copy code
fun notEmptyFieldInRange(from: UByte, to: UByte): Boolean {
        for ((f, c) in fields)
            if (f.value >= from && f.value <= to && !c.isEmpty)
                return true
        return false
    }
h
What's `f.value`s type?
n
if
f
is a UByte then you probably do not want to compare its
value
(which would be
Byte
)
n
f.value
is also
UByte
.
f
is my custom type.
h
The following snippet works fine. I'd verify
f.value
is a
UByte
as you believe.
Copy code
val f = 5u
val from = 0u
val to = 10u
if(f in from..to)
    println("works")
n
In your example all values are
UInt
. That will work indeed. Not in case of
UByte
.
f.value
is really
UByte
, doublechecked that.
h
Ah yes sorry, you just need to do
if (f.value.toUInt() in from..to && !c.isEmpty)
..
creates a
UIntRange
n
Thank you Brian, that worked!