Is there a way to match on two boolean variables a...
# functional
r
Is there a way to match on two boolean variables at the same time with a single
when
statement? Aka
Copy code
when (Pair(x, y)) {
  Pair(true, false) -> ...
}
r
Copy code
when {
    t && !f -> "."
    !t && f -> ".."
    else -> "..."
}
Something like that isn't ok?
y
Nothing built in, but you could roll your own:
Copy code
enum class BoolPair {
  FalseFalse
  FalseTrue
  TrueFalse
  TrueTrue
}
infix fun Boolean.alongWith(other: Boolean) = when {
  !this && !other -> FalseFalse
  !this && other -> FalseTrue
  this && !other -> TrueFalse
  else -> TrueTrue
}
//Somewhere else
when(x alongWith y) {
  TrueTrue -> doSomething()
  ...
  //No need for else branch 
}
k
Not as efficient as Youssef's implementation, but more fun to write:
Copy code
infix fun Boolean.alongWith(other: Boolean) = BoolPair.values()[(if (this) 2 else 0) + (if (other) 1 else 0)]
r
Copy code
when (x to y) {
  true to false -> ...
}
But stuffing everything into an ADT was the right choice ^^