了解列表上的模式匹配

我最近一直在玩提取器,并且想知道List提取器如何工作尤其如此:
List(1, 2, 3) match {
  case x :: y :: z :: Nil => x + y + z // case ::(x, ::(y, ::(z , Nil)))
}
Ok ::用在模式中,所以我猜编译器现在在:: - Object中查找unapply方法。试过这个:
scala> (::).unapply(::(1, ::(2, Nil)))
res3: Option[(Int, List[Int])] = Some((1,List(2)))
很好,有效。但是,这不是:
scala> (::).unapply(List(1,2,3))      
<console>:6: error: type mismatch;
 found   : List[Int]
 required: scala.collection.immutable.::[?]
       (::).unapply(List(1,2,3))
这样做:
scala> List.unapplySeq(List(1,2,3))
res5: Some[List[Int]] = Some(List(1, 2, 3))
其实我此刻有点困惑。编译器如何在此处选择正确的unapply实现。     
已邀请:
匹配基本上是做以下事情:
(::).unapply(List[Int](1,2,3).asInstanceOf[::[Int]])
一旦它知道它是安全的(因为
List(1,2,3).isInstanceOf[::[Int]]
true
)。     

要回复问题请先登录注册