caeus caeus - 3 years ago 199
Scala Question

How to assign a name to intermediate pattern of a List?

This code don't compile, what am I doing wrong? is it possible to do it?
How can I pattern match a list with at least 2 elements, and have the pattern have a variable for the tail (meaning

y :: _

I know it's possible desugaring the
or with a simple
. But without desugaring and without if... it's possible?

val list:List[Int] = ...
list match {
case x :: tail@(y:: _) =>

Answer Source

Try if this code works for you:

list match {
  case x :: (tail@(y :: _)) =>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download