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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fireproofsocks" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/120/7669_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fireproofsocks
                    <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>What would make more sense to me would be to drop the equals sign in the cases where we’re doing a kind of type-hint.</p>
<p>E.g. if we omitted the equals sign:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def foo(bar %Plug.Conn{}), do: "matched when input is a plug"
def foo(bar %Ecto.Changeset{}), do: "matched when input is a changeset"
def foo(bar %{status: "valid"}), do: "matched when input is a map with a status key with a value of valid"
# ... etc...
</code></pre>
<p>In all cases, <code>bar</code> gets the full input, it just happened to be filtered according to the type – conceptually something like a guard clause.</p>
<p>That would be more similar to how other languages do the type-hinting (e.g. Go and PHP, although PHP puts the type to the left of the variable).</p>
<p>There still is pattern matching going on there, but it’s not happening directly in the function, it’s happening immediately before when the kernel is choosing which function signature matches.  This only comes up in cases where you need to get the full value of a variable, but you need to some pre-emptive filtering.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120066" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-120066" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120066"
                     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="120534" data-post-id="120534">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="fireproofsocks" data-post="12" data-topic="20864">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/48/7669_2.png" class="avatar"> fireproofsocks:</div>
<blockquote>
<p>we’re doing a kind of type-hint</p>
</blockquote>
</aside>
<p>It’s not a kind of type hint.  You’re actually defining a head on the function that ONLY matches when passed data that matches the given pattern.  If no function head matches that pattern then the system will throw an exception.  To my way of thinking that’s much stronger than a “hint”.</p>
<p>The pattern match can also create bindings:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def foo(%{status: status} = whole_thing), do: "The status given was #{inspect status} and the whole thing is #{inspect whole_thing}"
</code></pre>
<p>There’s a lot more going on in that case than just providing a hint about what type should be used.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120534" data-batch-url="/posts/batch_likers">
                        4
                      </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/how-to-think-about-pattern-matching-vs-type-checking/20864/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-120534" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120534"
                     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="120543" data-post-id="120543">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fireproofsocks" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/120/7669_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fireproofsocks
                    <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>Yes, I know this is more than a type-hint.  I’m just trying to make sense of what I feel is a confusing syntax. When you start having to teach this stuff to coworkers and/or students, you become really sensitive to anything like this that creates mental friction and slows down understanding.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120543" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-120543" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120543"
                     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="120546" data-post-id="120546">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Pattern matching should be a new mind-bending feature to most new students of Elixir. One of the things I appreciated about the <em>Programming Elixir</em> book is that it covers that clearly and comprehensively right near the beginning. If you oversimplify that when teaching, then the student will never really feel comfortable with Elixir.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120546" data-batch-url="/posts/batch_likers">
                        4
                      </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/how-to-think-about-pattern-matching-vs-type-checking/20864/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-120546" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120546"
                     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="121603" data-post-id="121603">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fireproofsocks" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/120/7669_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fireproofsocks
                    <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 had a bit of an epiphany re the syntax involved here.  If I write out a series of matches like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(4)&gt; m = %{foo: foo} = %{foo: "bar"}
%{foo: "bar"}
iex(5)&gt; foo
"bar"
</code></pre>
<p>then it becomes more obvious how the matching works right to left.  You can see it will fail when the input (on the right) fails to be matched to the structures on its left.</p>
<p>If I squint, I can imagine that the right-most match is what happens when Elixir is figuring out which function to call (i.e. which function definition matches the value being passed).</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># myfunc(%{foo: "bar"})  &lt;--- matches
# myfunc(%{fizz: "buzz"})  &lt;--- does not match
def myfunc(m = %{foo: foo}), do: "My match!"
</code></pre>
<p>I’m probably just late to the party, but I thought I’d share.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="121603" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-121603" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="121603"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="121656" data-post-id="121656">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There’s no “right-most” match for function heads. The better mental model is what the compiler actually does with many function heads: move them into one case statement.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def myfunc(param_1) do
  case {param_1} do
    {m = %{foo: foo}} -&gt; "My match!"
    […]
  end
end
</code></pre>
<p>It’s even more apparent if you do some more esoteric matches like <code>%{foo: foo} = %{fizz: fizz}</code> in the function’s head. The only difference to actually writing it like that is a slightly different error if there’s no match, afaik.</p>
<p>The only part where location actually matters is for inline matches:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{foo: foo} = %{fizz: fizz} = %{foo: "test_1", fizz: "test_2"}
# foo = "test_1", fizz = "test_2"
%{foo: foo} = %{fizz: fizz} = %{foo: "test_1"}
# Fails on match
</code></pre>
<p>Here the right-most data is matched to everything on the left.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="121656" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-121656" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="121656"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="121795" data-post-id="121795">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes, the meaning of the <code>=</code> is different in patterns and in the inline use.</p>
<p>In the inline use the <code>=</code> actually has the syntax <code>pattern = expression</code> where the expression on the RHS is first evaluated and then the value of the expression is matched against the pattern. So it has a very strictly defined right-to-left semantics.</p>
<p>However, in a pattern it is an <code>alias</code> where both sides are pattern matched and both matches must succeed. All the variables in both patterns are bound if the matches succeed. It is commonly used for the case where you want to match and extract parts of a structure <strong>and</strong> have a reference to the whole. Like in you examples with maps and structs. It lets you have your cake and it.</p>
<p>Using the same operator perhaps wasn’t the smartest thing but it was inherited from Erlang so you can blame them (me). <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" 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="121795" data-batch-url="/posts/batch_likers">
                        10
                      </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/how-to-think-about-pattern-matching-vs-type-checking/20864/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-121795" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="121795"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="121935" data-post-id="121935">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Since the right-to-left semantics only apply to the rightmost term, these two are equivalent:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; %{x: a} = %{} = %{x: 1, y: 2}
iex&gt; %{} = %{x: a} = %{x: 1, y: 2}
</code></pre>
<p>So given the following function:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def f(%{x: a} = %{}) do
  a + 1
end

b = f(%{x: 1, y: 2})
</code></pre>
<p>if we were to inline it, it’s all consistent and makes sense:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{x: a} = %{} = %{x: 1, y: 2}
b = a + 1
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="121935" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-121935" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="121935"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="121968" data-post-id="121968">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="stefanchrobot" data-post="19" data-topic="20864">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/48/7657_2.png" class="avatar"> stefanchrobot:</div>
<blockquote>
<p>Since the right-to-left semantics only apply to the rightmost term</p>
</blockquote>
</aside>
<p>They applay to the whole term.</p>
<p>BUT, <code>%{} = %{x: 1, y: 2}</code> is an expression, returning the value given on the right.</p>
<p>This is, why have observed the behaviour you observed.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="121968" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-121968" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="121968"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="123143" data-post-id="123143">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yup, makes sense! So</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{x: a} = %{} = %{x: 1, y: 2}
</code></pre>
<p>is like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{} = %{x: 1, y: 2}
</code></pre>
<p>followed by:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{x: a} = %{x: 1, y: 2}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="123143" 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/how-to-think-about-pattern-matching-vs-type-checking/20864/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-123143" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="123143"
                     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 #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/20864/load_more?page=3">Load more posts (2 remaining)</a>
</div></template></turbo-stream>