scala - Why are variables not allowed in alternative patterns? -


often have "symmetric" matches , want write things like:

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

this not allowed, if every alternative has the same variables the same types, shouldn't problem, translated separate cases:

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

so why have restriction?

likely because take time implement , time better spent elsewhere. unnecessarily add complexity of language , compiler. mentioned, problem can avoided. 1 other way avoid problem write 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   } } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -