what this scala symbol

can someone assist me understanding this code

case "Foo" Foo(data) -> _ => { /*.. implementation */}

I see the usage of Foo.unapply(data) but I don't understand what this part

-> _

how and when to use it


It looks like someone is being way too clever for their own good. Suppose I've got the following:

case class Foo[A](command: String, data: A)

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

Now I can write this:

scala> Foo("foo", (42, 'whatever)) match {
     |   case "foo" Foo(data) -> _ => data
     | }
res0: Int = 42

Thanks to the magic of Scala's infix patterns, this is equivalent to the following:

Foo("foo", (42, 'whatever)) match {
  case Foo("foo", data -> _) => data
}

Except that the infix version is guaranteed to confuse and annoy your code's future readers.

链接地址: http://www.djcxy.com/p/72836.html

上一篇: 下划线造成困难

下一篇: 什么这个scala符号