Hello everyone, the compiler forced me to add an e...
# announcements
p
Hello everyone, the compiler forced me to add an else branch in the following snippet. What's the reason behind that?
Copy code
return when ((1..2).random()) {
            1 -> "ABC"
            2 -> "XYZ"
            else -> "???"
        }
The range only includes 1 and 2 and random returns an int.. 🤔
d
random()
returns an
Int
, as such as far as the compiler is concerned it could be any
Int
. Even more,
1..2
is just an Int range, it has no information about the size of that range.
👍 1
j
try
return when(1) { 1 -> "" }
as well for a more illustrative example. type ranges are not currently refined given compile-time information.
👍 1