Not maps. Or structs. You can’t expand those to their original value either. You’d have to call Code.eval_quoted() which is discouraged inside macros, no?
I’d like come back to my original question. I have changed the example to take a map. It does not make much sense like this and could be done better, but it is a simple example to show the case with a map as option value:
defmodule UsedModule do
defmacro __using__(opts) do
quote do
if unquote(opts[:with_extra]) do
def extra(), do: "with extra"
else
def extra(), do: "no extra"
end
end
end
end
defmodule UsingModuleWithExtra, do: use UsedModule, config: %{with_extra: true}
defmodule UsingModuleNoExtra, do: use UsedModule
iex> UsingModuleWithExtra.extra()
"with extra"
iex> UsingModuleNoExtra.extra()
"no extra"
See the passed option is expected to be a map. Which cannot be expanded to its original value. You have to unquote them inside a quote block (right?). So my question was, if you would - in this case where the argument requires unquoting - implement the macro like I did above or rather put the if condition inside the generated function like this:
def extra() do
if Keyword.get(opts, :config, %{})[:with_extra],
do: "with extra",
else: "no extra"
end


















