How to store defaults when __using__?

If I want to use a module to store some config on the calling module, is the best way to simply create a function that returns them? Right now I’m using a metadata struct as below. But I’m wondering if this is best approach, or whether I should be using module attributes instead somehow?

defmodule Client do
  use Options, name: "some-name", threshold: 10
end

defmodule Options do
  defmodule Metadata do
    defstruct [name: :none, threshold: 0]
  end
  
  defmacro __using__(opts) do
    quote do
      def __options_metadata__ do
        struct(Metadata, unquote(opts))
      end

      def fetch_options_meta(overrides \\ [])
        struct(__options_metadata__, overrides)
      end
    end
  end
end

Any insight would be appreciated.

Looks fine to me. Though inlining the structs instead of doing a struct call would allow the data to be fully interned, which in OTP20 even gets a bit of a speed boost, not that it matters, but eh. :slight_smile:

2 Likes