For example:
List(1,2,3,4) match {
case List(x: Int, y: Int, *rest) =>
println(rest) // i want to get List(3,4)
}
_* can match multiple variables but do to seem to be able to capture them.
Thanks!
You can use rest @ _* for this:
List(1,2,3,4) match {
case List(x: Int, y: Int, rest @ _*) =>
println(rest)
}
Note that this is general: you can use x @ pattern to give the name x to any value matched by pattern (provided this value has a suitable type). See http://scala-lang.org/files/archive/spec/2.11/08-pattern-matching.html#pattern-binders.
List(1,2,3,4) match { case ele1 :: ele2 :: rest => {} }