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
Post a Comment