Why is pattern sequence necessary?


#1

I’m wondering why unapplySeq was added, when unapply seems good enough?

Pattern sequence with “unapplySeq(…) : Option[Seq[…]]”:

case b() => …
case b(_) => …
case b(_,_) => …
case b(_,_,_*) => …

An equivalent pattern with “unapply(…) : Option[Seq[…]]”:

case b( Nil ) => …
case b( _ +: Nil ) => …
case b( _ +: _ +: Nil ) => …
case b( _ +: _ +: _ ) => …

The pattern sequence could be just a syntactic sugar for the latter, but Scala went further to add unapplySeq. Is there anything that can’t be achieved with unapply alone?
What was the thinking behind pattern sequence feature in Scala?


#2

I think I saw that dotty is phasing it out for Scala 3. I think the history lies in a more general regex-style pattern syntax (but I haven’t done that archaeology).

I think the future lies more along the lines of paulp’s reform.

I’m too lazy to look up the link for you, but I saw something very recently for dotty.