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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If there is a typespec in the function, dialyzer will fail  <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test do
  defstruct a: 0, b: nil, c: nil

  @type t :: %__MODULE__{a: non_neg_integer(), b: nil | String.t(), c: atom()}

  @spec foo() :: t
  def foo do
    %Test{
      a: 1,
      b: "aha",
      c: :foo
    }
    |&gt; Map.put(:bar, "bar")
  end
end
</code></pre>
<pre data-code-wrap="shell"><code class="lang-shell">$ mix dialyzer


lib/test.ex:6:invalid_contract
The @spec for the function does not match the success typing of the function.

Function:
Test.foo/0

Success typing:
@spec foo() :: %Test{:a =&gt; 1, :b =&gt; &lt;&lt;_::24&gt;&gt;, :bar =&gt; &lt;&lt;_::24&gt;&gt;, :c =&gt; :foo}
________________________________________________________________________________
</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="135977" 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/elixir-allows-map-functions-to-manipulate-structs/23826/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-135977" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="135977"
                     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="135994" data-post-id="135994">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Adding to this point wanted to say I found this library to be awesome for succinctly defining typespecs, enforced keys, and default values on structs so I now use it for every struct… <a href="https://github.com/ejpcmac/typed_struct" class="inline-onebox" rel="noopener nofollow ugc">GitHub - ejpcmac/typed_struct: An Elixir library for defining structs with a type without writing boilerplate code. · GitHub</a></p>
<p>It doesn’t solve all the runtime concerns of the OP but it helps dialyzer help you without too much ceremony!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="135994" 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/elixir-allows-map-functions-to-manipulate-structs/23826/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-135994" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="135994"
                     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="135996" data-post-id="135996">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>As I’ve written above, dialyzer errors properly on <code>Map.put/3</code>, guarding structs if you do use typespecs…</p>
<p>…However noticed that <code>Map.delete/2</code> would still pass, even if the required key defined in the struct typespec is missing <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>
<p>Example, the below returns an empty map after all, and still passes as it’s returning a <code>t</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @spec foo() :: t
  def foo do
    %__MODULE__{
      a: 1,
      b: "aha",
      c: :foo
    }
    |&gt; Map.delete(:a)
    |&gt; Map.delete(:b)
    |&gt; Map.delete(:c)
    |&gt; Map.delete(:__struct__)
  end
</code></pre>
<p>It isn’t a struct specific issue, a simple map also has the same issue (no errors):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @spec foo() :: %{required(:a) =&gt; non_neg_integer()}
  def foo do
    %{a: 1}
    |&gt; Map.delete(:a)
  end
</code></pre>
<p>I think it is a dialyzer bug, let’s see with the OTP guys what this behaviour turns out to be, bug or feature <img src="https://forum.elixirforum.com/images/emoji/apple/face_with_tongue.png?v=15" title=":face_with_tongue:" class="emoji" alt=":face_with_tongue:" loading="lazy" width="20" height="20"></p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.erlang.org/bugs/ERL-1002">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" alt="" width="32" height="32">

      <a href="https://www.erlang.org/bugs/ERL-1002" target="_blank" rel="noopener nofollow">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/c6d9d791b96b3afada2b395a4f19fbb45e20622022ad5de08b38dbf7d8fde8bc/erlang/otp/issues/3829" class="thumbnail" alt="" width="690" height="345"></div>

<h3><a href="https://www.erlang.org/bugs/ERL-1002" target="_blank" rel="noopener nofollow">ERL-1002: Dialyzer doesn't error when required atom keys were removed from a...</a></h3>

  <p>Original reporter: rodrigues Affected versions: OTP-22.0, OTP-21.3 Fixed in version: OTP-23.0 Component: dialyzer Migrated from: https://bugs.erlang.org/browse/ERL-1002 I believe the following beha...</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="135996" 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/elixir-allows-map-functions-to-manipulate-structs/23826/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-135996" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="135996"
                     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="144054" data-post-id="144054">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>A follow up on the issue described above: turns out it was really a bug in erlang, and Hans Bolinder from Ericsson fixed it today <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"> <a href="https://github.com/erlang/otp/pull/2392" class="inline-onebox" rel="noopener nofollow ugc">dialyzer: Handle maps:remove/2 better by uabboli · Pull Request #2392 · erlang/otp · GitHub</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="144054" 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/elixir-allows-map-functions-to-manipulate-structs/23826/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-144054" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="144054"
                     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>