title: Right way to produce elixir struct in macro
body: <p>I have the following design pattern: I have an elixir module, that responds to <em>growing/changing</em> amount of <code>functions/0</code>, called <code>Defaults</code>. I have also <code>CustomConfig</code> module, that is basically the struct, deriving defaults and assumed to be instantiated like:</p>
  
  <pre><code>%CustomConfig{ foo: "bar" }
  </code></pre>
  
  <p>where initialized properties are overwritten, others are taken from <code>Defaults</code> and those, not having a function with the same name in <code>Defaults</code> are rejected. So far, so good.</p>
  
  <p>To implement this behaviour independent on content (the list of functions in <code>Defaults</code>,) I use a macro (in other module, since one can not use macro, defined in the same module, within struct declaration):</p>
  
  <pre><code>defmacro define_struct_with_defaults do
    quote do
      defstruct Map.to_list(
        quote do: unquote(Enum.reduce(Dict.keys(
                          Defaults.__info__(:functions)), %{}, fn(k, acc) -&gt;
          Map.put(acc, :"#{k}", apply(Defaults, :"#{k}", []))
        end)))
    end
  end
  </code></pre>
  
  <p>While this works fine, I am pretty sure, that there should be more straightforward/elegant/less-cumbersome way to achieve this functionality.</p>
  
  <p>So my question would be: <strong>how one could declare <code>defstruct</code> from the <code>Map</code> without dancing this paso doble around map-reduce</strong>?</p>