I have a piece of code that looks like this: ``` ...
# announcements
p
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
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
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
Might look better with an enum class (Boolean, Boolean) anyway?
p
makes sense. Thanks
enum class does feel like a good idea…