Determining if an Elixir function has a specific arity

Cory O'Daniel
Aug 14, 2018 · 1 min read

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:

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]

CoryODaniel

Notes on SQL, ruby, elixir, tacos, and whiskey

Cory O'Daniel

Written by

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

CoryODaniel

Notes on SQL, ruby, elixir, tacos, and whiskey