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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What I have come to always do is just use ecto (if you’re not using it for DB you don’t need to bring in the postgres adapter) and just structure interaction points as casting schemas. This gives you a central place for documenting the types, constraints and form, allows you to use the validations in ecto (or write your own) in a pipeline fashion, allows you to define additional types to express fine-grained values automatically on cast, serialisation when going out back to whatever, and to then “type” your functions to only accept casted values  (ecto types) to work with - plus provides a very easy way to have fine grained error messages if needed.</p>
<p>It’s a bit more boilerplaty but I’ve found the end result to be way better than anything else I’ve seen (in and outside elixir) without much additional work. I even cast login params now, because why not.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="209997" data-batch-url="/posts/batch_likers">
                        5
                      </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/best-way-to-define-a-type-thats-only-a-limited-set-of-strings/6213/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-209997" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="209997"
                     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="210028" data-post-id="210028">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Okay, <strong>that</strong> is a design I can immediately see has a better structure than simple, parsed JSON. I think I might even try this if any of my projects develop complex enough entities.</p>
<p>We seem to have gotten onto a tangent, though; whether Ecto or structs result in more reliable coding patterns, I still believe IEx and hexdocs would benefit from string literals being acceptable input for typespecs <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" 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="210028" 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/best-way-to-define-a-type-thats-only-a-limited-set-of-strings/6213/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-210028" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="210028"
                     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="210030" data-post-id="210030">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="thepeoplesbourgeois" data-post="13" data-topic="6213">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thepeoplesbourgeois/48/21225_2.png" class="avatar"> thepeoplesbourgeois:</div>
<blockquote>
<p>I still believe IEx and hexdocs would benefit from string literals being acceptable input for typespecs</p>
</blockquote>
</aside>
<p>Yeah in a way I agree - I’ve tried it as well - I think the issue is dyalizer not supporting it (probably as a choice since binary pattern matching is supported in the vm), as such elixir can’t either because it would render wrong expectations as to what the type meant - since it can’t be enforced by dyalizer - you would have a spec but then it would be meaningless for other than pure documentation.</p>
<p>On the other hand it kinda forces you to pick up an enforceable representation (yeah sometimes it can be slightly annoying…) - with elixir proper you can use ecto and set custom types to your stringy things that you want to enforce, by casting them into valid atoms, which allows then to have proper typespecs. Structs are also much faster to serialise than stringed maps. But I can see a situation where, optimally and for documentation purposes, you may want to specify the keys a map has/can have as strings, for instance in an interface even if that then does proper casting itself.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="210030" 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/best-way-to-define-a-type-thats-only-a-limited-set-of-strings/6213/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-210030" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="210030"
                     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="242841" data-post-id="242841">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey peeps, I would have String Literal as well,</p>
<p>Imagine the following module,</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule GoodTypeProvider do
  use OnePiece.Commanded.TypeProvider

  register_mapping("something_happened", SomethingElseHappened)
  register_mapping("something_else_happened", SomethingHappened)
end
</code></pre>
<p>I would love to generate a typespec that as follows in the <code>GoodTypeProvider</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@spec to_string(SomethingHappened.t()) :: "something_happened"
</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="242841" 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/best-way-to-define-a-type-thats-only-a-limited-set-of-strings/6213/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-242841" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242841"
                     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="242852" data-post-id="242852">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is honestly no reason why this shouldn’t be possible by careful introspection of the bytecode.  At some point I’ll get time to finish up Mavis and maybe we’ll get this feature 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="242852" 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/best-way-to-define-a-type-thats-only-a-limited-set-of-strings/6213/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-242852" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242852"
                     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>