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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With