In case you have a static output like in this example then you can simply call unquote(string) and outside quote block set string variable based on option.
defmodule UsedModule do
defmacro __using__(opts) do
string = if opts[:with_extra], do: "with extra", else: "no extra"
quote do
def extra(), do: unquote(string)
end
end
end
In other case I would recommend to simply move if condition outside quote block and simply use 2 quote calls.
defmodule UsedModule do
defmacro __using__(opts) do
if opts[:with_extra] do
quote do
def extra(), do: "with extra"
end
else
quote do
def extra(), do: "no extra"
end
end
end
end
Note: If opts[:with_extra] does not exists then you got nil which is falsy value and therefore we do not need a Keyword.get/3 call here. ![]()


















