Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check a function's arity in Elixir?

Tags:

elixir

If I'm writing a function that gets passed another function, is there a way to check the arity of the function I'm passed, or pattern match against different arities? I could use is_function/2 to check for specific arities, but that would be an awkward way to get the number.

like image 493
Matt Avatar asked Dec 15 '15 13:12

Matt


1 Answers

You can use :erlang.fun_info/1; it returns a bunch of information about a given function, including its arity:

iex> :erlang.fun_info(fn -> :ok end)[:arity]
0
iex> :erlang.fun_info(fn(_, _, _) -> :ok end)[:arity]
3

As the documentation I linked says, this function is mainly intended for debugging purposes but it can be used to determine the arity of a function.

like image 55
whatyouhide Avatar answered Oct 02 '22 22:10

whatyouhide