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:

  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]

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade