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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Honestly? I think this is just one of those things you need to be paranoid about.</p>
<p>Comparing structs falls into an unfortunate local minima of Elixir’s design. There is a total ordering on its terms, so <code>a &lt; b</code> is <em>always</em> valid. So the compiler is a bit limited in what it can do. Though as <a class="mention" href="/u/garrison" rel="nofollow">@garrison</a> noted, you do get warnings in certain circumstances.</p>
<p>However, unless the compiler knows for some reason that the result of <code>&amp; &amp;1.inserted_at</code> will always be a <code>%NaiveDateTime{}</code>, there’s nothing for it to do. You could maybe do:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Enum.min_by(entries, fn %{inserted_at: %NaiveDateTime{} = ndt} -&gt; ndt end)
</code></pre>
<p>But at that point, you’re bending over backwards to get the compiler to tell you something you clearly already know. Though maybe if you use <a href="https://github.com/bamorim/typed_ecto_schema" rel="noopener nofollow ugc">TypedEctoSchema</a>, the compiler <em>would</em> be able to infer.</p>
<p>One other thought: always consider adding an <code>order_by</code> clause to your queries. Similar to this particular gotcha, it’s very easy to unknowingly rely on the order a query even though you didn’t specify it. E.g. we had a series of heisenbugs in our tests where I was blithely asserting on the result of <code>Repo.all()</code>. The results rows <em>usually</em> ordered the same, but not always.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="375551" 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/naive-datetime-values-comparison-using-and-operators/25455/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-375551" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="375551"
                     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="375553" data-post-id="375553">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="garrison" data-post="11" data-topic="25455">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/3bc359/48.png" class="avatar"> garrison:</div>
<blockquote>
<p>Structural comparison of dates was actually <a href="https://elixir-lang.org/blog/2024/06/12/elixir-v1-17-0-released/" rel="nofollow">used as an example</a> of the new type system</p>
</blockquote>
</aside>
<p>Oh that’s great, I must have missed that.<br>
I’ll need to do some experiments to see under which circumstances it can catch these things.<br>
I’m already on <code>1.19.0-rc.2</code>, so maybe if add some pattern matches on the <code>Repo.all()</code> results it might catch this.</p>
<aside class="quote no-group" data-username="billylanchantin" data-post="12" data-topic="25455">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/billylanchantin/48/28428_2.png" class="avatar"> billylanchantin:</div>
<blockquote>
<p>Though maybe if you use <a href="https://github.com/bamorim/typed_ecto_schema" rel="noopener nofollow ugc">TypedEctoSchema</a>, the compiler <em>would</em> be able to infer.</p>
</blockquote>
</aside>
<p>It looks like TypedEctoStruct helps to create dializer types, but I think those are ignored in the new type system, so this won’t help.<br>
Also, I think the source of the problem is that it can’t know the type of <code>Repo.all</code>.<br>
But I guess in theory it would be possible to infer the type from the query, so maybe the compiler can do it in the future.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="375553" 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/naive-datetime-values-comparison-using-and-operators/25455/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-375553" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="375553"
                     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="375556" data-post-id="375556">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Did anyone craft some macro-helper that lets use “&gt;” / “&lt;“ etc in a more direct way? “compare” is not intuitive to read <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" 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="375556" 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/naive-datetime-values-comparison-using-and-operators/25455/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-375556" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="375556"
                     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="375558" data-post-id="375558">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes: <a href="https://hexdocs.pm/compare_chain/readme.html" class="inline-onebox" rel="noopener nofollow ugc">CompareChain — compare_chain v0.6.0</a></p>
<p>The OP would be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">import CompareChain

datetime1 = ~N[2019-09-26 01:00:00.000000]
datetime2 = ~N[2020-01-24 00:00:00.000000]
compare?(datetime1 &gt; datetime2, NaiveDateTime) #=&gt; false
</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="375558" 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/naive-datetime-values-comparison-using-and-operators/25455/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-375558" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="375558"
                     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>