Determining if an Elixir function has a specific arity

Cory O'Daniel
CoryODaniel
Published in
1 min readAug 14, 2018

I’m working on a small PR for an open source project and I needed to figure out if a function supported a specific arity from the format {module, function} .

Elixir via Erlang can tell you a lot about a function using :erlang.fun_info/1 . This will allow you to:

iex(1)> :erlang.fun_info(&IO.puts/2)
[module: IO, name: :puts, arity: 2, env: [], type: :external]

Which is nice, but when you want to know if IO.puts supports a specific arity, its not helpful.

TIL there are two ways:

  1. Using :erlang.function_expored/3 which is useful to check for a specific arity.
iex(2)> :erlang.function_exported(IO, :puts, 7)
false
iex(3)> :erlang.function_exported(IO, :puts, 2)
true

2. Using metadata on the module and Keyword which is useful when you need to know all of the arities.

iex(4)> :functions |> IO.__info__ |> Keyword.get_values(:puts)
[1, 2]

--

--

Cory O'Daniel
CoryODaniel

SQL, kubernetes, elixir, ruby, node, tacos, whiskey, repeat.