Xaree Lee
Xaree Lee

Reputation: 3397

Elixir: is_function

I want to test some built-in function with is_function, but it fails:

> 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

How to test built-in function?

Upvotes: 2

Views: 1201

Answers (2)

Hauleth
Hauleth

Reputation: 23586

Apart to what @Dogbert said you also can use is_function(&is_function/1) to check if there is available such function in scope. Such syntax is required due to no difference between function name and variable name in Elixir.

Upvotes: 0

Dogbert
Dogbert

Reputation: 222388

You can use Kernel.function_exported?/3 and pass it the module name, function name, and arity to check:

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

(All functions that are callable in Elixir without importing any module, e.g. is_function or + are defined in the Kernel module.)

Upvotes: 7

Related Questions