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

后端 未结 1 1302
忘掉有多难
忘掉有多难 2021-01-12 02:36

E.g if I want to write

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

相关标签:
1条回答
  • 2021-01-12 03:22

    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
    
    0 讨论(0)
提交回复
热议问题