With regards to [p1 = %Product{}, p2 = %Product{}]
- the pattern match is essentially
[%Product{}, %Product{}] = Catalog.list_products
i.e. a two element list where each element has to be aProduct
structure - otherwise the match will fail. - after the match is successful the names
p1
andp2
are bound to their values.p1
is bound to the first element in the list andp2
is bound to the second element in the list.
Now consider [p1, p2] = Catalog.list_products
:
- the pattern match is essentially
[_, _] = Catalog.list_products
i.e. a two element list where each element can be any data type - otherwise the match will fail. - after the match is successful the names
p1
andp2
are bound to their values.p1
is bound to the first element in the list andp2
is bound to the second element in the list.
Finally [^p1, ^p2] = Catalog.list_products
- The match will only succeed if the first element of the list matches the current content of
p1
and the second element matches the current content ofp2
(and the list has exactly two elements).