How to convert varargs class constructor to Seq[T] constructor

Thanks for providing another argument to my list of reasons why not using varargs as a real type inside my code; it makes pattern matching more confusing.

Using varargs:

  • case Foo(_) => Means match a Foo that has only one element and ignore that element.
  • case Foo(x) => Means match a Foo that has only one element and assign that element to the x variable.
  • case Foo(ts @ _*) => Means match a Foo and collect in ts all the values inside it. - Note that the type of ts is Seq[T] but uses the same underlying class (and thus I hope same value, thus no copying) that it has inside it.

Using final case class Foo[T](ts: List[T]) then:
(Because, again, the point was not using Seq)

  • case Foo(_) => means match any Foo and ignore whatever value it has inside.
  • case Foo(ts @ _*) => doesn’t compile.
  • case Foo(List(ts @ _*)) means match any Foo and collect the values inside the List in ts - Note, the type of ts is Seq[T] but it seems to be returning the same underlying value (and thus class) that it has inside it.
  • case Foo(list) => means match any Foo and assign it the List it has inside in list, basically is the same as above but simpler. - Note, there is a difference in that list is of type List[T] which is better.
  • case Foo(_ :: Nil) => means match a Foo whose its underlying List has only one value and discard that value.
  • case Foo(x :: Nil) => means match a Foo whose its underlying List has only one value and assign the name x to that element.