I have a problem about assignment via pattern matching in scala. Let's say I have the following:
a. Seq.unapplySeq(List(1))
b. val Seq(z) = List(1)
c. val c = List(1) match {
case Seq(e) => e
}
d. List.unapplySeq(Seq(1))
e. val List(a) = Seq(1)
f. val b = Seq(1) match {
case List(e) => e
}
Only (d) doesn't compile and others compile and run right.
I know that unapplySeq of List is defined in SeqFactory as:
abstract class SeqFactory[CC[X] <: Seq[X] with GenericTraversableTemplate[X, CC]] extends GenSeqFactory[CC] with TraversableFactory[CC] {
applySeq[A](x: CC[A]): Some[CC[A]] = Some(x)
}
Because CC is List, Seq in (d) won't type check.
Seems like (a), (b) and (c) are in one group and (d), (e) and (f) are in the other.
In my understanding, destruction of (f) will actually call (d) because what the pattern matching in (f) does is use List to destruct Seq(1).
My question is why (e) and (f) are still right in the case (d) does not compile.