Ethan Gunderson

TIL: Accumulating Module Attributes in Elixir

Providing the option accumulate: true will cause subsequent calls to the attribute to accumulate instead of override. Basically turning the attribute into a small bit of mutable state from the perspective of the caller.

defmodule SomeModule do
  Module.register_attribute(__MODULE__, :opts, accumulate: true)

  @opts {:base_url, “example.com”}
  @opts {:timeout, 1_000}
  @opts #=> [{:base_url, “example.com”}, {:timeout, 1_000}]
end

#elixir #til