Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get ArrowAssoc work in pattern matching?

E.g if I want to write

1 -> 2 match {
  case 1 -> 2 => "matched"
  case _      => "not matched"
}
// error: not found: value ->

rather than the slightly less obvious

1 -> 2 match {
  case (1, 2) => "matched"
  case _      => "not matched"
}
like image 740
Luigi Plinge Avatar asked Mar 15 '12 18:03

Luigi Plinge


1 Answers

I have just such a thing! I like it because I find it more readable in many cases.

object -> {
  def unapply[A, B](pair: (A, B)): Option[(A, B)] =
    Some(pair)
}

Now you can do:

scala> val a -> b = 1 -> 2
a: Int = 1
b: Int = 2
like image 180
dhg Avatar answered Nov 15 '22 21:11

dhg