Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macros 如何“;splat&x201D;动态创建的函数的函数参数_Macros_Elixir_Metaprogramming - Fatal编程技术网

Macros 如何“;splat&x201D;动态创建的函数的函数参数

Macros 如何“;splat&x201D;动态创建的函数的函数参数,macros,elixir,metaprogramming,Macros,Elixir,Metaprogramming,我正在模块中生成一些函数: defmodule M do funs = [do_it: [:arg1, :arg2]] Enum.each(funs, fn {name, args} -> args = Enum.map(args, & {&1, [], Elixir}) def unquote(name)(unquote(args)), do: IO.inspect(unquote(args)) end) end 问题在于生成的

我正在模块中生成一些函数:

defmodule M do
  funs = [do_it: [:arg1, :arg2]]

  Enum.each(funs, fn {name, args} ->
    args = Enum.map(args, & {&1, [], Elixir})
    def unquote(name)(unquote(args)),
      do: IO.inspect(unquote(args))
  end)
end
问题在于生成的函数显然只接受一个参数,即大小为2的列表:

▶ M.__info__(:functions) 
#⇒ [do_it: 1]
目标是动态声明接受两个参数的函数。在ruby术语中,这将是unplat参数列表

是否有可能在不匹配
{:do_it,blah,[[list of arguments]]}
的结果AST的情况下完成此操作并手动展平列表?

您可以在
args
列表中使用“拼接”:

defmodule M do
  funs = [do_it: [:arg1, :arg2], do_it: [:arg1, :arg2, :arg3]]

  Enum.each(funs, fn {name, args} ->
    def unquote(name)(unquote_splicing(args)), do: :ok
  end)
end
iex(1)> M.__info__(:functions)
[do_it: 2, do_it: 3]