Understanding pattern matching on lists

3k views Asked by At

I've been playing around with Extractors lately and was wondering how the List extractors work especially this:

List(1, 2, 3) match {
  case x :: y :: z :: Nil => x + y + z // case ::(x, ::(y, ::(z , Nil)))
}

Ok :: is used in the pattern, so I guess that the compiler now looks up the unapply method in the ::-Object. So tried this:

scala> (::).unapply(::(1, ::(2, Nil)))
res3: Option[(Int, List[Int])] = Some((1,List(2)))

Nice that works. However this does not:

scala> (::).unapply(List(1,2,3))      
<console>:6: error: type mismatch;
 found   : List[Int]
 required: scala.collection.immutable.::[?]
       (::).unapply(List(1,2,3))

while this does:

scala> List.unapplySeq(List(1,2,3))
res5: Some[List[Int]] = Some(List(1, 2, 3))

Actually I'm a little puzzled at the moment. How does the compiler choose the right implementation of unapply here.

1

There are 1 answers

2
Rex Kerr On BEST ANSWER

Match is basically doing the following:

(::).unapply(List[Int](1,2,3).asInstanceOf[::[Int]])

once it knows that it's safe (because List(1,2,3).isInstanceOf[::[Int]] is true).