Why does unapplySeq involve case class or tuple in pattern matching

Viewed 272

I have a simple code that matches elements against a Seq whose length is 26.

  test("Seq Pattern matching") {
    val x = 1 to 26
    x match {
      case Seq(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) => println(a)
    }
  }

It will call Seq.unapplySeq method to de-structure the Seq object x,but I am surprised to see that the code causes compiling error:

Error:(82, 12) too many arguments for unapply pattern, maximum = 22
      case Seq(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) => println(a) 

In the unapplySeq method, it does NOT have any relationship with case class or tuple which have 22 fields limit.

So, I would ask why the error occurs(why case class/tuple fields limit causes this problem)

I am using Scala 2.11.8

1 Answers

I figured out what happens that underlie the unapplySeq method, it involves tuple,

For (1 to 10) match { case Seq(a, b, c, d, e, f, g, h, i, j) => (a, b, c); case _ => (1, 2, 3); }

something like:

scala.Predef.intWrapper(1).to(10) match {
  case scala.collection.Seq.unapplySeq[Int](<unapply-selector>) <unapply> ((a @ _), (b @ _), (c @ _), (d @ _), (e @ _), (f @ _), (g @ _), (h @ _), (i @ _), (j @ _)) => scala.Tuple3.apply[Int, Int, Int](a, b, c)
  case _ => scala.Tuple3.apply[Int, Int, Int](1, 2, 3)
}
Related