I am not sure I understand the requirements properly, but this is the raw implementation of the module holding map built in the compile time
defmodule AllInOne.Thing do
defstruct params: %{}
end
defmodule AllInOne.Registry do
@things %{
foo: %AllInOne.Thing{params: %{id: 1}},
bar: %AllInOne.Thing{params: %{id: 2}}
}
@spec things :: map()
def things, do: @things
@spec thing(atom()) :: %AllInOne.Thing{} | nil
def thing(name), do: Map.get(@things, name)
end
AllInOne.Registry.thing(:foo)
# ⇒ %AllInOne.Thing{params: %{id: 1}}
Here I built the map statically, but one might easily call another Elixir code there to build it dynamically. The only requirement, the called code should have been already compiled (it might be in the same project, but it cannot be in the same module, because modules get compiled “all-at-once”.)
Can a macro take an existing function, get the return data, and then overwrite it?
Sure it can. Here is an example from my Telemetría library, that grabs a function and wraps the call. You might want to explore this library source because it also modifies a code a lot during compile time.


















