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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="giddie" data-post="11" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p>which brings me back to - why no guards on match operators?</p>
</blockquote>
</aside>
<p>I have trouble imagining a scenario in my own code when I wouldn’t use a <code>case</code> for something that has more than one valid return value.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325604" 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/guard-clause-on-match-operator/62981/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-325604" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325604"
                     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="325606" data-post-id="325606">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Also think it’s rare for me to write a function that has multiple return <em>types</em> (as distinct from multiple return patterns).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325606" 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/guard-clause-on-match-operator/62981/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-325606" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325606"
                     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="325609" data-post-id="325609">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="DidactMacros" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  DidactMacros
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>{:ok, x} when match?(is_integer x, my_function())</p>
<p>The problem is this is a macro so these functions won’t be evaluated, but you can override the Kernel.match? With your own and using bindings you could have these parameters evaluated prior to being passed to the macro.</p>
<p>Inside your custom macro you would then use the Kernal.match? this will let you know whether the first arg value is of a matching pattern to the second.</p>
<p>Actually,</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
fun = fn 5 -&gt; [ok: 5] end

match? [ok: 5], fun(5)

true
</code></pre>
<p>works from Kernel, but match? is considered a “case” orderflow, so it won’t work with guards. I don’t know if a custom version will have the same outcome.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325609" 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/guard-clause-on-match-operator/62981/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-325609" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325609"
                     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="325615" data-post-id="325615">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="giddie" data-post="1" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Invalid Syntax
{:ok, x} when is_integer(x) = my_function()

# So I have to do:
{:ok, x} = my_function()
true = is_integer(x)
</code></pre>
</blockquote>
</aside>
<p>I’d rather write:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp unwrap_integer!({:ok, x}) when is_integer(x), do: x

x = my_function() |&gt; unwrap_integer!()
</code></pre>
<p>Instead of asking “why not”, I’d rather ask “how do I write what I want using what is available to me”</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325615" data-batch-url="/posts/batch_likers">
                        3
                      </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/guard-clause-on-match-operator/62981/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-325615" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325615"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="325679" data-post-id="325679">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="giddie" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/120/34394_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  giddie
                    <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">
								<aside class="quote no-group" data-username="Tyson" data-post="10" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tyson/48/33194_2.png" class="avatar"> Tyson:</div>
<blockquote>
<p>Contrast that to OTP’s approach of declaratively defining success cases and otherwise “letting it crash”</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="Tyson" data-post="12" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tyson/48/33194_2.png" class="avatar"> Tyson:</div>
<blockquote>
<p>I have trouble imagining a scenario in my own code when I wouldn’t use a <code>case</code> for something that has more than one valid return value.</p>
</blockquote>
</aside>
<p>Unless I’ve lost track of your train of thought, are you saying that a success case must always have more than one valid return value? I’m on board with the top quote, for sure: I like to define the success case, and for me that means sometimes I just want to specify that the data should be an integer when I extract it from a map, for instance:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{"my_key" =&gt; value} when is_integer(value) = my_map
</code></pre>
<p>I could use a case, for sure. But it would use two additional lines and add unnecessary noise. And I’m just wondering why this more concise syntax is not available.</p>
<p>Note that adding additional branches to a case here to handle an <code>other</code> case <em>would</em> be needlessly defensive - I don’t need to pattern-match an unexpected error case when I could just let it crash out.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325679" 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/guard-clause-on-match-operator/62981/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-325679" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325679"
                     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="325681" data-post-id="325681">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="giddie" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/120/34394_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  giddie
                    <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">
								<aside class="quote no-group" data-username="derek-zhou" data-post="15" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/derek-zhou/48/19943_2.png" class="avatar"> derek-zhou:</div>
<blockquote>
<p>I’d rather write:</p>
<p><code>defp unwrap_integer!({:ok, x}) when is_integer(x), do: x</code></p>
</blockquote>
</aside>
<p>OK, so that covers the integer case. Now you need to write the same function again for every possible combination <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"> Imagine I’m extracting an integer from a struct field, for instance?</p>
<aside class="quote no-group" data-username="derek-zhou" data-post="15" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/derek-zhou/48/19943_2.png" class="avatar"> derek-zhou:</div>
<blockquote>
<p>Instead of asking “why not”, I’d rather ask “how do I write what I want using what is available to me”</p>
</blockquote>
</aside>
<p>This does sound sensible, but actually I question the wisdom. Naturally, we can only use what we have available to use. That is a truism. But questioning why things are the way they are can lead us to improve the whole shebang for everyone.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325681" 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/guard-clause-on-match-operator/62981/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-325681" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325681"
                     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="325694" data-post-id="325694">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="giddie" data-post="17" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p>That is a truism.</p>
</blockquote>
</aside>
<p>I thought I helped?</p>
<p>You have your pet peeve and I have mine. This is free software so let the do’er decide.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325694" 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/guard-clause-on-match-operator/62981/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-325694" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325694"
                     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="325721" data-post-id="325721">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="giddie" data-post="16" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p>are you saying that a success case must always have more than one valid return value?</p>
</blockquote>
</aside>
<p>Definitely not!</p>
<aside class="quote no-group" data-username="giddie" data-post="1" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p><code>{:ok, x} = my_function()</code></p>
</blockquote>
</aside>
<p>I’m trying to imagine circumstances where return value <code>x</code> could have multiple possible types but you wouldn’t want to branch on them with <code>case</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325721" data-batch-url="/posts/batch_likers">
                        3
                      </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/guard-clause-on-match-operator/62981/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-325721" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325721"
                     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="325722" data-post-id="325722">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t understand the criticism towards OP here, especially by <a class="mention" href="/u/sodapopcan" rel="nofollow">@sodapopcan</a> and <a class="mention" href="/u/d4no0" rel="nofollow">@D4no0</a>.</p>
<p>Guards “should not” be used for type checking? Why not? How does it make the code bad or unreadable or untenable? Nobody is saying “do it on every assignment”. Do it at the function edges as <a class="mention" href="/u/zachallaun" rel="nofollow">@zachallaun</a> alluded to.</p>
<aside class="quote no-group" data-username="Tyson" data-post="12" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tyson/48/33194_2.png" class="avatar"> Tyson:</div>
<blockquote>
<p>I have trouble imagining a scenario in my own code when I wouldn’t use a <code>case</code> for something that has more than one valid return value.</p>
</blockquote>
</aside>
<p>Not sure I am not splitting hairs here because I might be taking your comment too verbatim – let me know (you might also include pattern-matching in function heads but I assumed you didn’t).</p>
<p>Still, picture my case, when a very old API returns prices in <em>three</em> different types because the old code had subtle bugs that nobody wants to fix, so it can be an integer, a float, or a string. So here’s an ancient helper I wrote (back in summer 2018) about dealing with this and normalizing the return into normal ok/error tuples:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def get_price(%{"currency" =&gt; currency, price =&gt; int}) when is_number(int) do
  {:ok, Decimal.new(int)}
end

def get_price(%{"currency" =&gt; currency, price =&gt; float}) when is_float(float) do
  {:ok, Decimal.from_float(float)}
end

def get_price(%{"currency" =&gt; currency, price =&gt; text}) when is_binary(text) do
  case Decimal.parse(text) do
    {decimal, ""} =&gt; {:ok, decimal}
    {_decimal, remnant} =&gt; {:error, :mixed_input}
    :error =&gt; {:error, :invalid_input}
  end
end
</code></pre>
<p>(Yeah, there’s <code>Decimal.cast</code> now. Back then it didn’t exist, here’s the PR that added it, and it even had a different name: <a href="https://github.com/ericmj/decimal/pull/116" class="inline-onebox" rel="noopener nofollow ugc">Add Decimal.from_any/1 function to handle int, float, and binary input by eqmvii · Pull Request #116 · ericmj/decimal · GitHub</a>)</p>
<hr>
<p>But overall I agree with <a class="mention" href="/u/zachallaun" rel="nofollow">@zachallaun</a> the most here: invariants can and should be coded (and at function edges) because they increase predictability and eliminate runtime errors. That’s not necessarily defensive programming. There’s a balance and I personally don’t lean on the “don’t check for anything” extreme.</p>
<p>Here’s an unpopular opinion: “let it crash” is neither a panacea nor the dominating technique to deal with the external world in production Erlang / Elixir code.</p>
<p>Yes, you are letting stuff crash <em>in the first iterations of your code</em> but once you find the erroneous case via your APM / telemetry system then you protect against it.</p>
<p>Is making sure your production code doesn’t fall over on its face on every invalid input “defensive programming”? Hope that’s not the claim that’s being put forward in defense of “don’t use guards for type checking”.</p>
<p>Or maybe I misunderstood the whole thing and you guys just meant “let it crash on unrecoverable errors” in which case I’ll immediately agree.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325722" data-batch-url="/posts/batch_likers">
                        2
                      </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/guard-clause-on-match-operator/62981/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-325722" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325722"
                     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="325733" data-post-id="325733">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was trying to get further involved but now you pinged me, d’oh <img src="https://forum.elixirforum.com/images/emoji/apple/upside_down_face.png?v=15" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="dimitarvp" data-post="20" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/48/38664_2.png" class="avatar"> dimitarvp:</div>
<blockquote>
<p>Guards “should not”</p>
</blockquote>
</aside>
<p>I never said “should.”  OP asked why things are the way they are and I was trying to provide an answer.  People are free to write code how they want.  Also, OP is asking why we can’t do guards in <em>inline</em> matches.  You example uses <code>case</code>.</p>
<p>In short, <a class="mention" href="/u/zachallaun" rel="nofollow">@zachallaun</a> said what I meant but far more eloquently.  By “not meant for type assertions” I meant that they are “meant” for flow control.  And I don’t buy that they are 100% same thing, though of course they are very closely related.  For example in LiveView, José <em>recommends</em> (not a “commands”) matching only against keys that will satisfy a match:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_event("foo", %{"id" =&gt; id}, socket) do
  current_user = socket.assigns.current_user
</code></pre>
<p>as opposed to something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_event("foo", %{"id" =&gt; id}, %{assigns: %{user: user}} = socket) do
</code></pre>
<aside class="quote no-group" data-username="giddie" data-post="7" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p>We must be talking at crossed purposes, because I’d say that OTP is pretty much the embodiment of defensive programming.</p>
</blockquote>
</aside>
<p>It’s already been touched on but your definition of defensive programming is not the “regular” one and <a href="https://www.erlang.org/faq/academic#idm1387" rel="nofollow">it’s certainly not Erlang’s definition</a>.  <em>Generally</em> when we do inline matches it’s on stuff that can fail either because it’s a side effect or the result of parsing user input:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">:ok = network_request()
{:ok, result} = parse_user_input_i_wont_deal_with_if_its_wrong_for_some_reason("hi!")
</code></pre>
<p>You otherwise don’t usually see random <em>type</em> assertions in a <em>function body</em>.  I’m actually having a hard time picturing when this would even be useful—why wouldn’t you check it in the function head?  In any event, if you have a use-case that’s fine but again <em>I was just trying to answer the question</em>.  There is a direct example of how this is not <em>intended</em> in the link above but program however you want.</p>
<aside class="quote no-group quote-modified" data-username="giddie" data-post="7" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<aside class="quote no-group" data-username="zachallaun" data-post="6" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachallaun/48/29208_2.png" class="avatar"> zachallaun:</div>
<blockquote>
<p>I also feel like invariants should be checked at boundaries, not scattered throughout the code.</p>
</blockquote>
</aside>
<p>Yeah, I definitely agree with this. But I’d also 100% prefer a pattern match error near some unexpected data over some obscure <code>key :whatever not found in: nil</code> waaay down in the stack.</p>
</blockquote>
</aside>
<p>This is sort of missing the point—if you’ve checked at the boundaries and you’re getting a <code>nil</code> error, your boundary-checking is wrong.  The whole point of boundary casting is to get data into a a solid, known shape (ideally a struct AFAIC though whole other convo).</p>
<aside class="quote no-group" data-username="giddie" data-post="7" data-topic="62981">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/giddie/48/34394_2.png" class="avatar"> giddie:</div>
<blockquote>
<p>I’d go so far as to say that guards semantically <em>are</em> pattern matches. They offer programmable extension to patterns.</p>
</blockquote>
</aside>
<p>Agreed.  The docs even touch on this.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="325733" 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/guard-clause-on-match-operator/62981/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-325733" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="325733"
                     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/62981/load_more?page=3">Load more posts (5 remaining)</a>
</div></template></turbo-stream>