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 aFoo
that has only one element and ignore that element.case Foo(x) =>
Means match aFoo
that has only one element and assign that element to thex
variable.case Foo(ts @ _*) =>
Means match aFoo
and collect ints
all the values inside it. - Note that the type ofts
isSeq[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 anyFoo
and ignore whatever value it has inside.case Foo(ts @ _*) =>
doesn’t compile.case Foo(List(ts @ _*))
means match anyFoo
and collect the values inside theList
ints
- Note, the type ofts
isSeq[T]
but it seems to be returning the same underlying value (and thus class) that it has inside it.case Foo(list)
=> means match anyFoo
and assign it theList
it has inside inlist
, basically is the same as above but simpler. - Note, there is a difference in thatlist
is of typeList[T]
which is better.case Foo(_ :: Nil) =>
means match aFoo
whose its underlyingList
has only one value and discard that value.case Foo(x :: Nil) =>
means match aFoo
whose its underlyingList
has only one value and assign the namex
to that element.