I want to case match tuples which contain boolean values. Is there an effective way to match the results as case match on boolean looksexhautive
val ABC= "abc"
val DEF = "def"
private def istest1 = true
private def istest2 = true
private def istest3 = true
(istest1, istest2, istest3) match {
case (true, true, true)=>ABC
case (false, true, true) =>DEF
case (false , false , true)=> //print something else
//other cases
}
You need to have a statement for each of the possible outcomes, and using match
seems to be the most compact and readable way to do it.
One possible improvement is to use _
for "don't care" values:
(istest1, istest2, istest3) match {
case (true, true, true) => ABC
case (_, true, true) => DEF
case (false, _, true) => //print something
case _ => //other cases
}
There may be performance issues with different versions of these tests, but it is best to pick the one that makes the most sense. Aim for readability and maintainability above perceived performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With