Is there a reason that match
written against Seq
would work differently on IndexedSeq
types than the way it does on LinearSeq
Followed pattern match works for List
, Seq
, LinearSeq
, IndexedSeq
, Vector
.
Vector(1,2) match {
case a +: as => s"$a + $as"
case _ => "empty"
}
You can't use ::
for anything other than List
. The Vector
is failing to match because ::
is a case class that extends List
, so its unapply
method does not work for Vector
.
val a :: b = List(1,2,3) // fine
val a :: b = Vector(1,2,3) // error
But you can define your own extractor that works for all sequences:
object +: {
def unapply[T](s: Seq[T]) =
s.headOption.map(head => (head, s.tail))
}
So you can do:
val a +: b = List(1,2,3) // fine
val a +: b = Vector(1,2,3) // fine