Reputation: 83411
I can create a list like so:
val value = List(1) ::: 2 :: List(3)
Now I am trying to decompose that list like so:
value match { case a ::: b :: c => (a, b, c) }
but I get error: not found: value :::
.
Why I am getting this error. Why doesn't this pattern work, and what should I use instead?
Upvotes: 1
Views: 1089
Reputation: 16328
Suppose you have
val xs = List(1, 2, 3, 4)
and suppose there is an extractor object that could extract a collection prefix. What should be matched for
case a ::: b :: c => (a, b, c)
Is it (choose all that apply)
(List(1, 2, 3), 4, Nil)
(List(1, 2), 3, List(4))
(List(1), 2 , List(3, 4))
(Nil, 1, List(2, 3, 4))
Because there is more than one way of matching the pattern, the above extractor cannot exist. Instead you could use the following.
value match { case a :: b :: c => (List(a), b, c) }
Upvotes: 3