Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way to case match a tuples with boolean values in scala

Tags:

scala

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
} 
like image 604
coder25 Avatar asked Oct 19 '25 08:10

coder25


1 Answers

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.

like image 164
Tim Avatar answered Oct 21 '25 22:10

Tim