Elixir:is_function

李岡諭*_*李岡諭 2 elixir

我想测试一些内置函数is_function,但它失败了:

> add = fn a, b -> a + b end
#Function<12.118419387/2 in :erl_eval.expr/5>

> is_function add
true

> is_function is_function    # test itself
warning: variable "is_function" does not exist and is being expanded to "is_function()", please use parentheses to remove the ambiguity or change the variable name
  iex:27
Run Code Online (Sandbox Code Playgroud)

如何测试内置功能?

Dog*_*ert 6

您可以使用Kernel.function_exported?/3并传递模块名称,函数名称和arity来检查:

iex(1)> function_exported?(Kernel, :is_function, 1)
true
iex(2)> function_exported?(Kernel, :is_function, 2)
true
iex(3)> function_exported?(Kernel, :is_function, 3)
false
iex(4)> function_exported?(Kernel, :function_exported?, 3)
true
Run Code Online (Sandbox Code Playgroud)

(所有可在Elixir中调用的函数,无需导入任何模块,例如is_function+Kernel模块中定义.)