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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ffloyd" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ffloyd/120/10538_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ffloyd
                    <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="sodapopcan" data-post="30" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>Unless you are doing something like <code>Repo.get(Struct, 1)</code> everywhere (web layer, api layer) instead of going through a domain function <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>
</blockquote>
</aside>
<p>That’s a right question. For simplicity let me provide one related example:</p>
<p>We have shared queries - because domains want to preload things defined in other domain. Or sql-join with them. While subquery is defined inside domain A, it becomes part of the query in domain B and now domain B should also take care of initializing <em>virtual field defaults</em>. As result I need to put dynamic defaults in every domain that generates my struct indirectly via query.</p>
<p>And we can easily have dozens of functions that creates your struct directly or indirectly in a single domain of our big monolith.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377450" 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/runtime-calculated-default-values-for-structs/73189/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-377450" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377450"
                     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 #31"></div>
  </section>
</div>
    <div class="postbit" id="377453" data-post-id="377453">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="ffloyd" data-post="8" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ffloyd/48/10538_2.png" class="avatar"> ffloyd:</div>
<blockquote>
<aside class="quote no-group" data-username="mudasobwa" data-post="6" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudasobwa/48/5298_2.png" class="avatar"> mudasobwa:</div>
<blockquote>
<p>I have been developing enough code to strictly avoid messing with the standard syntax.</p>
</blockquote>
</aside>
<p>I cannot do this on a big production project and dependencies. Elixir articles and books already suggest to use factory functions when you need dynamic default. But the problem is when in 100+ places you already use <code>%Struct{...}</code> explicitly or implicitly. And when libraries do “no dynamic defaults” restriction just because they want to reuse struct defaults.</p>
</blockquote>
</aside>
<p>Could throw together a custom credo check that warns on <code>%Struct{...}</code> when <code>Struct.new/build</code> is detected, perhaps. A community consensus on <code>new</code> vs <code>build</code> could be handy as well.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377453" data-batch-url="/posts/batch_likers">
                        6
                      </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/runtime-calculated-default-values-for-structs/73189/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-377453" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377453"
                     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="377466" data-post-id="377466">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes. For me the split I’m observing comes down to people who feel that code which calls functions should be a function call, and this proposal where doing literal syntax is actually sometimes a way to call functions dynamically. You can tell from this framing which side I’m on I suppose lol.</p>
<p>But more seriously some of the objections to using <code>new</code> such as the time required to convert  code base don’t hold water. Sure we are talking about dynamic data now but what about invariants? Should struct literal syntax also run validators on the values? The answer is: no, we already crossed this bridge with changesets. (And no the type system won’t solve this, there are invariants not amenable to types).</p>
<p>If we had a convention around new or build then this would be a non issue. You wouldn’t have 100 spots in your code to refactor you’d have 100 function calls and you make one change to the function. This is already what you have when using changesets.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377466" 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/runtime-calculated-default-values-for-structs/73189/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-377466" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377466"
                     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 #33"></div>
  </section>
</div>
    <div class="postbit" id="377469" data-post-id="377469">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I agree with everyone who has said that “it takes to long to refactor” is not a very compelling argument. It would be nice if there was a more “blessed” constructor pattern though. It would be great to know that if I am initializing a struct I can/should always call `new` or whatever. Even better if it came with a way to prevent “private” fields from being set somehow…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377469" 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/runtime-calculated-default-values-for-structs/73189/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-377469" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377469"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="377474" data-post-id="377474">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>These are exactly my thoughts. This entire thread wouldn’t be needed if constructors were the default way to create structs.</p>
<p>I’m convinced that it’s perfectly fine to have constructor functions that only pass the data into a struct. Some will become more complex later, others will stay trivial, but the changes will never spread outside of the module.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377474" 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/runtime-calculated-default-values-for-structs/73189/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-377474" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377474"
                     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 #35"></div>
  </section>
</div>
    <div class="postbit" id="377476" data-post-id="377476">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="tfwright" data-post="35" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tfwright/48/20299_2.png" class="avatar"> tfwright:</div>
<blockquote>
<p>a more “blessed” constructor pattern though</p>
</blockquote>
</aside>
<p>FWIW, the aforementioned estructura provides <a href="https://hexdocs.pm/estructura/Estructura.html#coerce/3" rel="noopener nofollow ugc">coerce/3</a> which calculates (also coerces and validates btw) everything because it goes through <code>Access</code> which is amending the calculated fields. <a href="https://github.com/am-kantox/estructura/blob/master/examples/with_formulae/test/with_formulae_test.exs#L11-L15" rel="noopener nofollow ugc">Example</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="377476" 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/runtime-calculated-default-values-for-structs/73189/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-377476" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377476"
                     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 #36"></div>
  </section>
</div>
    <div class="postbit" id="377477" data-post-id="377477">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="benwilson512" data-post="34" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>no the type system won’t solve this, there are invariants not amenable to types</p>
</blockquote>
</aside>
<p>There are none, if we use dependent types. Sorry for nitpicking, but inventing new wheels for a wheelchair, like ad-hoc validations. would never cover SMT-solving theories. Ad-hoc validations are extremely hard to validate in the wild, unlike a proof in languages like Idris. That’s basically why I started <a href="https://erlangforums.com/t/cure-a-new-language-compiled-to-beam/" rel="nofollow">Cure</a> effort.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377477" 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/runtime-calculated-default-values-for-structs/73189/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-377477" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377477"
                     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 #37"></div>
  </section>
</div>
    <div class="postbit" id="377480" data-post-id="377480">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ffloyd" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ffloyd/120/10538_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ffloyd
                    <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="tfwright" data-post="35" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tfwright/48/20299_2.png" class="avatar"> tfwright:</div>
<blockquote>
<p>I agree with everyone who has said that “it takes to long to refactor” is not a very compelling argument.</p>
</blockquote>
</aside>
<p>I feel you. Such refactoring wasn’t painful for me until I started working on 2M+ LOC Elixir codebase. It’s one of the biggest (if not the biggest) production monolith written in Elixir. In any other codebase I touched, this problem was in “meh, annoying!” category, not “it’s too long to refactor”.</p>
<aside class="quote no-group" data-username="Sorc96" data-post="36" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sorc96/48/28361_2.png" class="avatar"> Sorc96:</div>
<blockquote>
<p>This entire thread wouldn’t be needed if constructors were the default way to create structs.</p>
</blockquote>
</aside>
<p>100% percent agree! That’s the problem I’m trying to actually solve! Bear with me:</p>
<p>The reality I see is that <code>%Schema{}</code> syntax is used to “create struct with defaults”. My strongest proof that even Ecto does it and proposes it. If Ecto were enforcing using <code>Schema.build(...)</code> as <em>the idiomatic way</em> to create a schema instance - it would have dynamic defaults already.</p>
<p>I believe that allowing static defaults in struct definition was a design mistake (that was done for Erlang records, structs merely repeat it). If setting static default were require making a <code>build</code>/<code>new</code> function - Ecto would have <code>build</code>/<code>new</code> functions enforced.</p>
<p>It made me think about making <code>%Schema{}</code> a full-featured constructor. But it violates <code>%A{} == %A{}</code>, so, not a good way.</p>
<aside class="quote no-group" data-username="benwilson512" data-post="34" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>Should struct literal syntax also run validators on the values?</p>
</blockquote>
</aside>
<p>My latest proposal <strong>is not about %MakeMeDynamic{}</strong> anymore.</p>
<aside class="quote no-group" data-username="benwilson512" data-post="34" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p>If we had a convention around new or build then this would be a non issue.</p>
</blockquote>
</aside>
<p>Not exactly. What I’m fighting with right now is the idea that <em>static and dynamic defaults should be implemented in different ways.</em> Ideally, all my problems would be solved by:</p>
<blockquote>
<p>Remove ability to set static defaults. Any kind of defaults should require implementing <code>new</code>/<code>build</code> function.</p>
</blockquote>
<p>But it’s too late for this proposal.</p>
<p>Should I make a proposal to Ecto then? To make Ecto create <code>build</code> function when defining schema and suggest people to use it over <code>%Schema{}</code> syntax if they need <em>any</em> kind of defaults? It’s a breaking change. How would you estimate chances that people agree on this? (Considering that in the most cases lack of dynamic defaults are not super painful)</p>
<p>Not every Elixir developer uses Ecto. And the design mistake of not enforcing usage of <code>build/new</code> when a struct can have dynamic defaults has a good chance to be repeated in other libraries.</p>
<p><em>That’s why I want to solve this problem on the language syntax level.</em> It’s more bulletproof solution than relying on traditions and conventions.</p>
<details>
<summary>
My latest proposal is</summary>
<aside class="quote no-group" data-username="ffloyd" data-post="26" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ffloyd/48/10538_2.png" class="avatar"> ffloyd:</div>
<blockquote>
<p>What about having a slightly different data-type then: <em>dynamically created struct</em>. It will work just like normal structs, support <code>struct!(...)</code>, protocols, etc. The only difference is that you define it using <code>defdynstruct</code> and cannot create them as literal, you must use something like <code>%MyStruct()</code> and <code>%MyStruct(collectable)</code> to instantiate them?</p>
<p>Also, <code>%MyStruct(...)</code> should work for normal structs also.</p>
<p>(Yes, we already have <code>struct!</code> and can use this instead of <code>%MyStruct(...)</code>, but it feels like worse UX to me).</p>
<p>Then:</p>
<ul>
<li>Static defaults via defstruct:
<ul>
<li>part of a struct definition</li>
<li>guaranteed to be applied</li>
<li>introducing the first static default does not require refactoring</li>
</ul>
</li>
<li>Dynamic defaults via defdynstruct:
<ul>
<li>part of a struct definition</li>
<li>guaranteed to be applied (you cannot do <code>%MyStruct{}</code>)</li>
<li>introducing the first dynamic default requires refactoring, but more trivial one:
<ul>
<li>in the struct it’s a mere replacement of <code>defstruct</code> with <code>defdynstruct</code></li>
<li>in the code you own it’s a mere replacement of <code>%MyStruct{...}</code> to <code>%MyStruct(...)</code></li>
<li>in libraries: you only need to switch to using <code>struct!(...)</code> in places where you work with user-provided structs and avoid do <code>%unquote(struct_mod){}</code> hacks in macro.</li>
</ul>
</li>
</ul>
</li>
</ul>
</blockquote>
</aside>
</details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377480" 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/runtime-calculated-default-values-for-structs/73189/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-377480" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377480"
                     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 #38"></div>
  </section>
</div>
    <div class="postbit" id="377541" data-post-id="377541">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You sparked a really good discussion. Thanks for that.</p>
<blockquote>
<p>The fact that <code>default: DateTime.utc_today()</code> is resolved at compile time actually also a surprise of a similar level.</p>
</blockquote>
<p>True, but that is really part of the language. It’s like the <code>@tag</code> that also gets evaluated at compile time.</p>
<p>You are right that you could learn this too. The difference is that, once you have learned ot, you don’t get surprises, whereas for your structs I would need to check the focumentation every time. This is the kind of surprise I’m talking about.</p>
<p>As a language you need to be conservative when adding new features to the language (consider the <a href="https://dannorth.net/blog/how-simple-is-too-simple/" rel="noopener nofollow ugc">Change Event Horizon</a>). Hence we ahould ask ourselves how common your problem is, and whether it could be solved on a different level before solving it on the language. Here some options I could see:</p>
<ul>
<li>Solve the problem on a database level, e.g. at database insertion/retrieval time (and I think that would be cleanest, because you solve the problem locally)</li>
<li>Add a feature to Ecto that allows the initialization of default/calculated values. Maybe an <code>_init: &amp;myinit/1</code> field in your struct?</li>
</ul>
<p>Even though I might be against your proposal, I very much appreciate you taking the time to raise it. As more use cases surface, the more likely it is that a solution might happen on a language level.</p>
<p>Side note:</p>
<blockquote>
<p>If even creators of Ecto (and ExMachina) fell into the trap</p>
</blockquote>
<p>It’s up to them to decide that. As demonstrated above there are plenty of ways on how you can extend keep things extendable. And at the end it’s always a) a balancing act and b) a learning process</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377541" 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/runtime-calculated-default-values-for-structs/73189/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-377541" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377541"
                     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 #39"></div>
  </section>
</div>
    <div class="postbit" id="377734" data-post-id="377734">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ffloyd" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ffloyd/120/10538_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ffloyd
                    <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="a-maze-d" data-post="40" data-topic="73189">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/a-maze-d/48/31720_2.png" class="avatar"> a-maze-d:</div>
<blockquote>
<p>it could be solved on a different level before solving it on the language.</p>
</blockquote>
</aside>
<p>Agree. So I made a library as an outcome of the idea and discussion here. Let me know WDYT about it!</p>
<p><a href="https://github.com/ffloyd/better_struct" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ffloyd/better_struct</a></p>
<p>(will do post in libraries-related part of this forum after getting initial feedback and publishing on hex).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377734" 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/runtime-calculated-default-values-for-structs/73189/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-377734" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377734"
                     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>