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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ffloyd" data-post="21" 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>Meanwhile I’m curious, what would be you idea of implementing dynamic default with Ecto? Patching all the functions that uses schema or can have this schema module as an argument? Or do I miss something more clever?</p>
</blockquote>
</aside>
<p>Find all the places, which need that behaviour and make them call a function setting those dynamic defaults. Anything short of that is “hope for the best / that nothing broke”. Introducing that new behaviour is no different to any other change you add to your codebase. That you consider those dynamic field values “defaults” imo doesn’t change that.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377409" 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/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-377409" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377409"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="377410" data-post-id="377410">
  <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="ffloyd" data-post="15" 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>Is it adequate price for introducing dynamic default?</p>
</blockquote>
</aside>
<p>Absolutely. This would have the issue with wrongly declared/used structs fixed, rather than swept it under the rug.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377410" 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/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-377410" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377410"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="377411" data-post-id="377411">
  <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="LostKobrakai" data-post="22" 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/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>Find all the places, which need that behaviour and make them call a function setting those dynamic defaults</p>
</blockquote>
</aside>
<p>So, if we’re talking about a huge codebase, it means:</p>
<ul>
<li>do it in dozens or potentially hundreds of places (with Ecto virtual fields we need also patch <em>each</em> place where we read from the storage).</li>
<li>do not forget to include this functionality in any new functions that create such struct (isn’t it error prone in huge projects?)</li>
</ul>
<p>Does still look acceptable? =)</p>
<aside class="quote no-group" data-username="LostKobrakai" data-post="22" 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/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>Introducing that new behaviour is no different to any other change you add to your codebase</p>
</blockquote>
</aside>
<p>I do not agree that “introducing dynamic default” is like any other change. There is a concept of a default value. When you think about it from general design perspective “is it dynamic or not?” is an implementation detail. What I see as a problem is that static and dynamic ones has very different ways of being implemented.</p>
<p>Looks like you ok with the idea that “dynamic defaults should not be part of a struct definition”. It makes dynamic default an opt-in behavior, not a guaranteed one. So, we have the following inconsistency:</p>
<ul>
<li>Static defaults:
<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:
<ul>
<li>separate from a struct definition</li>
<li>has no application guarantees (it’s up to developer to find all the places where to inject them)</li>
<li>introducing the first dynamic default requires refactoring and searching for workarounds when 3rd party libraries involved.</li>
</ul>
</li>
</ul>
<p>This inconsistency is more destructive from my point of view than accepting <code>%A{} != %A{}</code>. If you can hint a way that will resolve this inconsistency without violating <code>%A{} == %A{}</code> that would be awesome.</p>
<p>I personally like Elixir for easiness of refactoring of business code. So argument “yes, refactoring is expensive just accept it” goes against things I love Elixir for. =)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377411" 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/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-377411" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377411"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="377414" data-post-id="377414">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t think we’ll find any alignment here. I still stand by the fact that structs should behave like native datatypes, while you seem to want to make them even more a special case to what they already are and partially out of necessity. I see <code>%Struct{}</code> as a notation to define data and data doesn’t execute code. Yes one can change the definition of the struct, but generally people try to avoid having the before and after of those definitions meet.</p>
<p>Execution is btw another footgun to your proposal. <code>%Struct{}</code> executing code means different runtime behaviour. It’s no longer “O(1)”, but could start an http request, fail somewhere on the path and raise some arbitrary exception crashing a whole process – just from <code>%Struct{}</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="377414" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-377414" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377414"
                     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="377417" data-post-id="377417">
  <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">
								<p>In the spirit of brainstorming:</p>
<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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377417" 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/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-377417" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377417"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="377428" data-post-id="377428">
  <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>What is the use-case around <em>not</em> having a dynamic values auto-generated just before insertion?  I also don’t understand why would you want to have dynamically generated default values populated on <em>read</em>.</p>
<p>It would be interesting to hear motivation beyond the <code>DateTime</code> one.  As dates go, I’m having a hard time imaging a case where you would ever want any other time than at insertion.  The only thing I can think of is “when did the user start filling out this form,” though I think that could only reliably be tracked on the client.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377428" 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/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-377428" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377428"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="377431" data-post-id="377431">
  <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="27" 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>What is the use-case around <em>not</em> having a dynamic values auto-generated just before insertion?</p>
</blockquote>
</aside>
<p>If this question is scoped to Ecto, then:</p>
<ul>
<li>When a field is a subject for validation. Or part of a condition of a conditional validation. Validation happens before insertion.</li>
<li>When a field is used to calculate another field value before insertion.</li>
<li>When there is no persistence (virtual fields, when schema is used only for validation, like in Oban jobs).</li>
</ul>
<aside class="quote no-group" data-username="sodapopcan" data-post="27" 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>why would you want to have dynamically generated default values populated on <em>read</em>.</p>
</blockquote>
</aside>
<p>We have a schema that resembles “booking restrictions config”. It has “current allowed dates period” <em>virtual</em> field. By default it should be from beginning of the current year to the end of it. Even if you <em>read</em> a saved one from DB.</p>
<p>Meanwhile, they’re cases when we need to override it. In such cases we override this virtual field.</p>
<p>This virtual field is used in validations. It used during rendering things for JSON responses, etc. It used in many places when the entity is used. It is very <em>convenient</em> to have it as a virtual field. Otherwise we need to add one additional argument/option to too many functions.</p>
<p>Consider that I’ve significantly simplified related business logic.</p>
<p>While adding this additional argument may look a right way to do, on practice it will make implementation uglier and more fragile. It still possible, but I asked myself - why I have to do such ugly things in my favorite language? (When in Rails or Golang it would be a one-liner).</p>
<p>I realized that many times I needed a dynamic default, but each time instead of using this simple concept I was forced to do a workaround. Sometimes spending significant time on a thing that should be simple. But it was… acceptable. Then I found this case where I wasn’t able to fix it even after couple of hours. And I realized that it’s probably easier to create a library that adds dynamic defaults to Elixir. And here we’re. =)</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="27" 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>hear motivation beyond the <code>DateTime</code> one</p>
</blockquote>
</aside>
<p>Hope that example above works)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377431" 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/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-377431" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377431"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="377436" data-post-id="377436">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just thinking out lout here and the whole thing is a matter of year or even two. But could this maybe be solved (at least partially) by the future <a href="https://github.com/elixir-lang/elixir/issues/13881" rel="noopener nofollow ugc">typed structs</a>? If you would declare your field as being <code>DateTime</code> (for example). You would then be forced everywhere you’re trying to create this struct to provide that value? The ergonomics wouldn’t be ideal, but at least there won’t be any missed case.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377436" 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/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-377436" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377436"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="377445" data-post-id="377445">
  <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">
								<aside class="quote no-group" data-username="ffloyd" data-post="28" 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>While adding this additional argument may look a right way to do, on practice it will make implementation uglier and more fragile. It still possible, but I asked myself - why I have to do such ugly things in my favorite language? (When in Rails or Golang it would be a one-liner).</p>
</blockquote>
</aside>
<p>Sorry I don’t want to pile too much onto the detractor side but I think this in a nutshell is the problem.  Again I would have to see code but I don’t know why something like would be ugly or why it would have to change in so many places no matter how big the project.  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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377445" 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/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-377445" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377445"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="377447" data-post-id="377447">
  <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="stefanluptak" data-post="29" 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/stefanluptak/48/34398_2.png" class="avatar"> stefanluptak:</div>
<blockquote>
<p>You would then be forced everywhere you’re trying to create this struct to provide that value?</p>
</blockquote>
</aside>
<p>Hmm. Interesting! It will work for many cases but I’m afraid not for Ecto virtual fields: Ecto creates struct instances inside their helper (<code>MyStruct.__schema__(:loaded)</code>). The helper is generated by <code>schema do ... end</code> macro. It will try to create a record with defaults from <code>field/3</code> definitions and I haven’t found any documented way to override it.</p>
<p>Might be I need to be more explicit of my goal here: It’s not about finding a way how to reliably refactor in order to introduce a dynamic default.</p>
<p><em>I want to find a way to avoid refactoring when I introduce a dynamic default to an existing struct in a big (500k+ lines) project. And find a way to define static and dynamic defaults using similar syntax, both in Elixir stdlib and libraries.</em></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="377447" 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/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-377447" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="377447"
                     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 #30"></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/73189/load_more?page=4">Load more posts (10 remaining)</a>
</div></template></turbo-stream>