Reputation: 48460
I'm reviewing some Scala code trying to learn the language. Ran into a piece that looks like the following:
case x if x startsWith "+" =>
val s: Seq[Char] = x
s match {
case Seq('+', rest @ _*) => r.subscribe(rest.toString){ m => }
}
In this case, what exactly is rest @ _*
doing? I understand this is a pattern match
for a Sequence, but I'm not exactly understanding what that second parameter in the Sequence is supposed to do.
Was asked for more context so I added the code block I found this in.
Upvotes: 1
Views: 207
Reputation: 15074
If you have come across _*
before in the form of applying a Seq as varargs to some method/constructor, eg:
val myList = List(args: _*)
then this is the "unapply" (more specifically, search for "unapplySeq") version of this: take the sequence and convert back to a "varargs", then assign the result to rest
.
Upvotes: 5
Reputation: 54999
x @ p
matches the pattern p
and binds the result of the whole match to x
. This pattern matches a Seq
containing '+'
followed by any number (*
) of unnamed elements (_
) and binds rest
to a Seq
of those elements.
Upvotes: 2