Kotlin - Type inference in unsigned type ranges

Viewed 30

Why does this work (using signed types)...

val x = 123
println(x in Byte.MIN_VALUE..Byte.MAX_VALUE)

...but this doesn't (using unsigned types)...

val x = 123
println(x in UByte.MIN_VALUE..UByte.MAX_VALUE)

Error produced is:

Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.

1 Answers

So Byte.MIN_VALUE..Byte.MAX_VALUE creates an IntRange (there is no ByteRange). And x is an Int here so it can work.

UByte.MIN_VALUE..UByte.MAX_VALUE returns a UIntRange, so you would need to declare x as a UInt type:

val x = 123U
Related