Runtime-calculated default values for structs

Yeah. Let me summarize:

  1. Often engineers do %Struct{} for creating a new instance of a struct by default. There is no established tradition to create factory functions like in Golang.
  2. Elixir allows to set only “static” defaults. So %Struct{} already has some implicitness, but at least it is “idempotent” after compilation.
  3. For defining a dynamic default each place where %Struct{}, struct(...), etc is used should be refactored.
    • In big codebases it can lead to inadequately expensive refactoring for just introducing a mere dynamic default.
    • creation of a struct can be inside library code so you may be not able to inject default value at all.
  4. Cultural problem: libraries repeat “no dynamic defaults” restriction that does not makes developers life easier, usually opposite.
  5. Such limitation is uncommon in other popular languages I can recall.

Original proposal solves mentioned problems, but breaks %Struct{} == %Struct{} behavior. But without breaking it we cannot make %Struct{} respect dynamic defaults. And without this we cannot make adding dynamic defaults cheap in big codebases.

:red_question_mark: Might be loosing this behavior is acceptable sacrifice? Or we’re missing some smart move here?