Hmm. Interesting! It will work for many cases but I’m afraid not for Ecto virtual fields: Ecto creates struct instances inside their helper (MyStruct.__schema__(:loaded)). The helper is generated by schema do ... end macro. It will try to create a record with defaults from field/3 definitions and I haven’t found any documented way to override it.
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.
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.






















