vendredi 8 janvier 2021

scala pattern matching on value in sequence of strings

variable someKey can be either "a", "b" or "c".

I can do this:

someKey match {
    case "a" => someObjectA.execute()
    case "b" => someOther.execute()
    case "c" => someOther.execute()
    case _ => throw new IllegalArgumentException("Unknown")
}

how can I compress this pattern matching so I can check if someKey with e.g. Seq("b", "c") and if it is in the sequence then replace two lines of pattern match with one?

Aucun commentaire:

Enregistrer un commentaire