【发布时间】:2016-09-01 06:24:20
【问题描述】:
假设我有一个模块 Silent 和 Definer。我想根据其属性为Silent 定义几个函数。让我解释一下:
defmodule Silent do
@function_names [:a, :b, :c]
use Definer
end
defmodule Definer do
defmacro __using__(_) do
quote do
Enum.each(@function_names, fn(n) ->
def unquote(n)() do # line 5
IO.puts "a new method is here!"
end
end)
end
end
end
但这种方法实际上不起作用,因为我有undefined function n/0 on line 5。如何实现所需的功能?
【问题讨论】:
-
我很想知道这个用例是什么。您是否正在尝试做某种穷人的界面?如果是这样,您可能应该查看 Elixir 的协议:elixir-lang.org/getting-started/protocols.html
标签: metaprogramming elixir phoenix-framework ecto