How to call an Erlang function in Elixir
Asked Answered
I

3

14

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.

Infundibuliform answered 9/2, 2016 at 4:27 Comment(0)
I
18

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.

Infundibuliform answered 9/2, 2016 at 4:27 Comment(3)
The point is you call the functions normally, the only difference is that Elixir modules have CamelCaseNames while erlang modules have :snake_case_names_with_a_colonDecrepitate
Don't forget the important point @Gerry that Elixir strings which are denoted with "string" need to be changed to char lists when passed to Erlang like 'string' I'd add that to your answer.Miltiades
@gerry the updated URL for the link to the OTP Reference Page Index is: erlang.org/doc/man_index.html . I attempted to update your answer, but I got the "too many edits in queue" error.Conformity
E
4

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.

Entrechat answered 10/2, 2016 at 19:40 Comment(0)
B
1

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.

Bander answered 11/2, 2016 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.