我想动态创建一个函数名。我写了这个宏
defmacro generate_dynamic(name) do
quote do
def add_(unquote(name)) do
end
end
end
我这样使用它:
defmodule AnotherModule do
generate_dynamic :animal
end
现在,我只定义AnotherModule.add_
函数,而我期望AnotherModule.add_animal
函数。
答案 0 :(得分:42)
要实现此目的,您可以在取消引用之前将:add_
添加到名称之前。此外,在这种情况下,方法名称后面的括号需要防止歧义。这应该可以解决问题:
defmacro generate_dynamic(name) do
quote do
def unquote(:"add_#{name}")() do
# ...
end
end
end
答案 1 :(得分:21)
有时作为有用的快捷方式,您可以在不使用非引用片段编写宏的情况下实现相同的内联结果。
defmodule Hello do
[:alice, :bob] |> Enum.each fn name ->
def unquote(:"hello_#{name}")() do
IO.inspect("Hello #{unquote(name)}")
end
end
end
Hello.hello_bob # => "Hello bob"
Hello.hello_alice # => "Hello alice"
答案 2 :(得分:5)
我在一个要点中尝试模仿Ruby的attr_accessor
:
defmodule MacroExp do
defmacro attr_accessor(atom) do
getter = String.to_atom("get_#{atom}")
setter = String.to_atom("set_#{atom}")
quote do
def unquote(getter)(data) do
data |> Map.from_struct |> Map.get(unquote(atom))
end
def unquote(setter)(data, value) do
data |> Map.put(unquote(atom), value)
end
end
end
defmacro attr_reader(atom) do
getter = String.to_atom("get_#{atom}")
quote do
def unquote(getter)(data) do
data |> Map.from_struct |> Map.get(unquote(atom))
end
end
end
end
defmodule Calculation do
import MacroExp
defstruct first: nil, second: nil, operator: :plus
attr_accessor :first # defines set_first/2 and get_first/1
attr_accessor :second # defines set_second/2 and get_second/1
attr_reader :operator # defines set_operator/2 and get_operator/1
def result(%Calculation{first: first, second: second, operator: :plus}) do
first + second
end
end