Landei Landei - 2 months ago 14
Scala Question

Why are variables not allowed in alternative patterns?

Often you have "symmetric" matches and want to write things like:

def g(p:(Int,Int)) = p match {
case (10,n) | (n,10) => println(n)
case _ => println("nope")
}


This is not allowed, but if every alternative has the same variables with the same types, this shouldn't be a problem, as it could be translated to separate cases:

def g(p:(Int,Int)) = p match {
case (10,n) => println(n)
case (n,10) => println(n)
case _ => println("nope")
}


So why do we have this restriction?

Answer

Likely because it would take some time to implement and that time is better spent elsewhere. It would also unnecessarily add to the complexity of the language and its compiler. As you already mentioned, the problem can easily be avoided. One other way to avoid the problem is to write a custom extractor:

object ThisOrThat {
  def unapply(p:(Int,Int)):Option[Int] = p match {
    case (10, n) => Some(n)
    case (n, 10) => Some(n)
    case _ => None
  }
}