如何通过指定模块和方法名称在Elixir中动态调用方法?

hal*_*elf 34 erlang metaprogramming elixir

我想知道elixir中的方法名称究竟是什么:

array = [1,2,3]
module_name = :lists
method_name = :nth                  # this not working
module_name.method_name(1, array)   # error, undef function lists.method_name/2
module_name.nth(1, array)           # returns 1, module_name is OK. It's an atom
Run Code Online (Sandbox Code Playgroud)

但我可以在erlang中做同样的事情:

A = [1,2,3].
X = lists.
Y = nth.
X:Y(1,A).  #  returns 1
Run Code Online (Sandbox Code Playgroud)

我怎么能在灵药中做到这一点?

las*_*nal 50

你可以使用apply/3哪个只是一个包装:erlang.apply/3.它只是一个数组调用给定的给定.functionmodulearguments由于您将参数作为模块和函数名称传递,因此可以使用变量.

apply(:lists, :nth, [1, [1,2,3]])
apply(module_name, method_name, [1, array])
Run Code Online (Sandbox Code Playgroud)

如果你想更多地了解elixir如何处理函数调用(以及其他所有内容),你应该看看quoteunquote.

contents = quote do: unquote(module_name).unquote(method_name)(1, unquote(array))
Run Code Online (Sandbox Code Playgroud)

它返回函数调用的homoiconic表示.

{{:.,0,[:lists,:nth]},0,[1,[1,2,3]]}
Run Code Online (Sandbox Code Playgroud)

您可以unquote使用引用的函数调用Code.eval_quoted/3

{value, binding} = Code.eval_quoted(contents)
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个使用Enum.fetch和var的例子.

quoted_fetch = quote do: Enum.fetch([1,2,3], var!(item));             
{value, binding} = Code.eval_quoted(quoted_fetch, [item: 2])
Run Code Online (Sandbox Code Playgroud)