Consider the following code:
%{a_left: a_left} = a_full = %{} = a_full = %{a_right: a_right} = %{a_left: 42, a_right: 42}
#⇒ %{a_left: 42, a_right: 42}
Is every element in the chain is always matched against the rightmost one? Is there any reference/link/documentation all around describing this behavior? The official doc is pretty concise
# Operators
This document covers operators in Elixir, how they are parsed, how they can be defined, and how they can be overridden.
## Operator precedence and associativity
The following is a list of all operators that Elixir is capable of parsing, ordered from higher to lower precedence, alongside their associativity:
Operator | Associativity
---------------------------------------------------------------------------------------- | -------------
`@` | Unary
`.` | Left to right
`+` `-` `!` `^` `not` `~~~` | Unary
`*` `/` | Left to right
`+` `-` | Left to right
`++` `--` `..` `<>` | Right to left
`^^^` | Left to right
`in` `not in` | Left to right
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left to right
`<` `>` `<=` `>=` | Left to right
This file has been truncated. show original
This shows right to left as precedance, so do put everything on the left onto parenthesis starting from the right and you‘ll explicitly see the order of application. Also the pattern match expression will always evaluate to the full matched value. So yes everything will match the rightmost value.
3 Likes
Indeed. I somehow missed that.
FYI:
Yes, this is how it works. It means that each pattern will be matched against the term. There is an implicit grouping (Pattern0 = (Pattern2 = … (PatternN = Term)…)) so if variables occur in many the binding behaves as the matching goes from the inside out. Fro example:
iex(1)> {a,b} = {b,a} = {3,4}
{3, 4}
iex(2)> a
3
iex(3)> b
4
iex(4)> {b,a} = {a,b} = {3,4}
{3, 4}
iex(5)> a
4
iex(6)> b
3
If you pin (’^’ ) a variable in any of the patterns then this importing of the value occurs before any mat…
1 Like