https://kotlinlang.org logo
#announcements
Title
# announcements
p

pavel

10/07/2019, 5:47 PM
I have a piece of code that looks like this:
Copy code
fun pairTest(foo: Boolean, bar: Boolean) =
        when (foo to bar) {
            (true to true) -> 1
            (true to false) -> 2
            (false to true) -> 3
            (false to false) -> 4
        }
IntelliJ is saying that I need an else branch here. How come? And is there a better way to do this?
In my world, a pair of booleans can only have 4 different possible values
s

streetsofboston

10/07/2019, 5:51 PM
The compiler is just not that smart….. An exhaustive when class only works with enums and sealed classes. And even then, you can not get complex in your
when
conditions.
r

raulraja

10/07/2019, 5:52 PM
Boolean is not implemented in Kotlin as a sealed union and the compiler doesn't know that your match covers all cases in the Pair.
a

Andy Gibel

10/07/2019, 5:53 PM
Might look better with an enum class (Boolean, Boolean) anyway?
p

pavel

10/07/2019, 5:55 PM
makes sense. Thanks
enum class does feel like a good idea…
2 Views