12
votes

What is the format to call an Erlang function in Elixir?

Specifically, how can I call the function in iex and where can I find a list of modules and functions available from Erlang.

3

3 Answers

16
votes

First find the module and function you want to call in the Erlang OTP Reference Page Index.

For example to call random uniform you would look in the random module and find the uniform\0 function.

To call that in Elixir you use the format, :module.function(), e.g.,

iex(1)> :random.uniform()
0.7230402056221108

For functions that do not take any parameters the parenthesis are optional.

4
votes

The autocomplete in iex will help a lot with this.

iex> :c<TAB> 

will show you all the loaded modules from Erlang that start with the letter c, and

iex> :crypto.<TAB>

will show you all the functions available in that module. Unfortunately as of Elixir 1.2 the h command does not yet work for Erlang modules. It does have a useful side effect though.

Not all the available Erlang modules are loaded initially (there are over 500 in the standard Erlang distribution). One way to get a module loaded is to use the h command.

iex> h :crypto

Or you could just use the l command, but that's not nearly as fun.

1
votes

erldocs.com allow you to interactively search the Erlang documentation.

Manpages are also convenient (just man <module-name> in your shell), if you have them installed. For this, I recommend kerl, which can install Erlang manpages automatically with the right config.

Shameless plug to my project: h on Erlang functions / modules in IEx doesn't work, but the hope is it will thanks to docsh.