<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="358624" data-post-id="358624">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mentero" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mentero/120/24698_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mentero
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t think you can “give” precedence to the pattern match with <code>( )</code>. I think they will just be ignored</p>
<p>In your example <code>(%{a: x} = %{b: y}) = %{a: "what", b: "happened"}</code> is a pattern match where the rightmost value is <code>%{a: "what", b: "happened"}</code>, so it has to be known and can’t bind variables, and the left side is <code>(%{a: x} = %{b: y}) </code> which is a pattern that the mentioned value has to satisfy. It means that the value need to satisfy both <code>%{a: x}</code> and <code>%{b: y}</code> and it will check against <code>%{b: y}</code> first before doing <code>%{a: x}</code>. That means that if the match fails on both patterns, the execution will stop and error on the <code>b</code> match first, but in order for match to succeed, all the patterns on the left side need to be matched.</p>
<p>So you are not comparing <code>%{a: x}</code> to <code>%{b: y}</code> - these are patterns for the same input. The only way when two patterns on the left side can interrupt each other is when they bind to the same variable, as there is a rule that value can be bind multiple times in one expression only if it binds to the same value. So <code>%{a: x} = %{b: x} = %{a: "what", b: "happened"}</code> will fail</p>
<p><strong>EDIT:</strong><br>
Ok. No longer so sure enough about this statement to give tips on how it actually works. I simply always perceived match operator as folding from right to left, checking patterns in the process but it might be just my mental model</p>
<blockquote>
<p>and it will check against <code>%{b: y}</code> first before doing <code>%{a: x}</code>. That means that if the match fails on both patterns, the execution will stop and error on the <code>b</code> match first, but in order for match to succeed, all the patterns on the left side need to be matched.</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358624" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-358624" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358624"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="358631" data-post-id="358631">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Parashoe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Parashoe/120/37738_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Parashoe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My intuition matches yours. Conceptually <code>(%{a: x} = %{b: y})</code> is itself a pattern which intersects <code>%{a: x}</code> and <code>%{a: y}</code>. This pattern is then matched with the value on the right, and it’s probably implemented by transforming the order of execution as if it wasn’t there.</p>
<p>It’s just that this behavior is not specified or documented anywhere I can see. There are no other operators producing patterns for the left side like this right? This is a very special case?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358631" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-358631" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358631"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="358632" data-post-id="358632">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="D4no0" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/D4no0/120/33624_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  D4no0
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Erlang pattern matching is inspired by prolog. In prolog you can have bindings on both sides of match and it will try to solve it like an equation. I think erlang also supports a limited version of this behavior.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358632" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-358632" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358632"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="358635" data-post-id="358635">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Parashoe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Parashoe/120/37738_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Parashoe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Okay I made a small module of two matches and got the lowered erlang code out:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MatchOperatorExamination do
  def left_precedence do
    (%{a: "foo", b: "bar"} = %{c: "qux"}) = %{a: "foo", b: "bar", c: "qux"}
  end

  def default_precedence do
    %{a: "foo", b: "bar"} = %{c: "qux"} = %{a: "foo", b: "bar", c: "qux"}
  end
end
</code></pre>
<p>Compiled and extracted using <code>:beam_lib.chunks</code> and then pretty printing following this guide: <a href="https://medium.com/learn-elixir/disassemble-elixir-code-1bca5fe15dd1" rel="noopener nofollow ugc">Disassemble Elixir code. And check if Erlang dead code… | by Gaspar Chilingarov | Learn Elixir | Medium</a></p>
<pre data-code-wrap="erl"><code class="lang-erl">default_precedence() -&gt;
    #{a := &lt;&lt;"foo"&gt;&gt;, b := &lt;&lt;"bar"&gt;&gt;} = #{c := &lt;&lt;"qux"&gt;&gt;} =
                                            #{a =&gt; &lt;&lt;"foo"&gt;&gt;, b =&gt; &lt;&lt;"bar"&gt;&gt;,
                                              c =&gt; &lt;&lt;"qux"&gt;&gt;}.

left_precedence() -&gt;
    (#{a := &lt;&lt;"foo"&gt;&gt;, b := &lt;&lt;"bar"&gt;&gt;} = #{c :=
                                               &lt;&lt;"qux"&gt;&gt;}) =
        #{a =&gt; &lt;&lt;"foo"&gt;&gt;, b =&gt; &lt;&lt;"bar"&gt;&gt;, c =&gt; &lt;&lt;"qux"&gt;&gt;}.
</code></pre>
<p>By the time the expressions get to the BEAM they still have the parenthesis on the left hand side. So my question is as much an Erlang one as it is an Elixir one.</p>
<aside class="quote no-group" data-username="D4no0" data-post="14" data-topic="69871" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>Erlang pattern matching is inspired by prolog. In prolog you can have bindings on both sides of match and it will try to solve it like an equation. I think erlang also supports a limited version of this behavior.</p>
</blockquote>
</aside>
<p>Erlang syntax does look Prolog-esque! especially with the dashes for declarations. I’ll be dogged if pattern matching is anything like horn clauses though. That said, Erlang pattern matching seems to have a little more than meets the eye given what we’re looking at here.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358635" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-358635" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358635"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="358637" data-post-id="358637">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bjorng" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/120/13187_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bjorng
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>A few years ago we in the compiler team at OTP started to get bug many bug reports about the Erlang compiler crashing when compiling some “strange” Erlang code. It turned out that the Erlang code that triggered the crashes was generated by a fuzzer, <a href="https://github.com/WhatsApp/erlfuzz" rel="noopener nofollow ugc">erlfuzz</a>.</p>
<p>One of the classes of bugs in the Erlang compiler and in the documentation was in pattern matching. We had many discussions in the OTP team about how to best describe pattern matching and how to fix the compiler to be consistent and never crash.</p>
<p>The result was that we realized that there is not one match operator, but two distinct operators: the <em>match operator</em> and the <em>compound match operator</em>.</p>
<p>Here is the revised documentation about matching resulting from those bug reports and our discussions:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.erlang.org/doc/system/expressions.html#the-match-operator">
  <header class="source">

      <a href="https://www.erlang.org/doc/system/expressions.html#the-match-operator" target="_blank" rel="noopener nofollow">erlang.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://www.erlang.org/doc/system/expressions.html#the-match-operator" target="_blank" rel="noopener nofollow">The Match Operator - Expressions — Erlang System Documentation v29.0.2</a></h3>

  <p>In this section, all valid Erlang expressions are listed. When writing Erlang programs, it is also allowed to use macro and record expressions. However, these expressions are expanded during compilation and are in that sense not true Erlang...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358637" data-batch-url="/posts/batch_likers">
                        9
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-358637" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358637"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="358647" data-post-id="358647">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Parashoe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Parashoe/120/37738_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Parashoe
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m reading it now. I think this concurs with my reading of the Elixir School article and the match is distributed over the left compound pattern: <a href="https://www.erlang.org/doc/system/expressions.html#the-match-operator-and-the-compound-pattern-operator" rel="nofollow">The Match Operator and the Compound Pattern Operator</a> “Since the construct inside the parentheses is a pattern, the <code>=</code> that separates the two patterns is the compound pattern operator (<em>not</em> the match operator). The match fails because the two sub patterns are matched at the same time”</p>
<aside class="quote no-group" data-username="Parashoe" data-post="9" data-topic="69871">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/parashoe/48/37738_2.png" class="avatar"> Parashoe:</div>
<blockquote>
<p>I read this to mean an expression like this: <code>(person = fred) = (%{name: person_name} = fred)</code> is happening opposed to: <code>person = (%{name: person_name} = fred)</code> although I think the latter is in fact correct.</p>
</blockquote>
</aside>
<p>Assuming the function argument pattern is compound, it actually is more like the former and the article has been right this whole time!<br>
Let me double check with the example in Elixir:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(38)&gt; m = %{key: :key2, key2: :value}
%{key: :key2, key2: :value}
iex(39)&gt; %{k =&gt; :value} = %{key: k} = m
error: cannot use variable k as map key inside a pattern. Map keys in patterns can only be literals (such as atoms, strings, tuples, and the like) or an existing variable matched with the pin operator (such as ^some_var)
└─ iex:39

** (CompileError) cannot compile code (errors have been logged)

iex(39)&gt; %{^k =&gt; :value} = %{key: k} = m
error: undefined variable ^k. No variable "k" has been defined before the current pattern
└─ iex:39

** (CompileError) cannot compile code (errors have been logged)

iex(39)&gt; (%{^k =&gt; :value} = %{key: k}) = m
error: undefined variable ^k. No variable "k" has been defined before the current pattern
└─ iex:39

** (CompileError) cannot compile code (errors have been logged)
</code></pre>
<p>It seems Elixir prevents the edge case where the order of operation makes a difference by preventing the variable being used as a map key within the pattern. But I take it from here that the matches really are independent under the hood, and that this nuance almost never matters.</p>
<p><strong>Thank you so much, this was really bugging me</strong><br>
Well onto lesson 7 <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="358647" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/understanding-the-match-operator/69871/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-358647" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="358647"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>