Using Erlang libs in Elixir

145 views
Skip to first unread message

Serge Aleynikov

unread,
Sep 1, 2021, 7:04:09 PM9/1/21
to Erlang Questions
Is there any guideline for making Erlang projects be more Elixir friendly?

When developing an Erlang library, it would be nice to have its functions callable from Elixir not like:

:erl_lib.some_fun()

but rather

ErlLib.some_fun()

without needing to write a "glue" module compiled into 'Elixir.ErlLib.beam'  that would import all functions from erl_lib and export them locally?

Are there more requirements of some metadata (e.g. '__info__'/1) to be included in the Erlang modules?

Regards,

Serge

Dominic Morneau

unread,
Sep 1, 2021, 11:19:54 PM9/1/21
to Serge Aleynikov, Erlang Questions
Telemetry is a good example of an Elixir-friendly Erlang lib, especially the docs: https://github.com/beam-telemetry/telemetry/

Some of the general Elixir library guidelines here also apply in Erlang: https://hexdocs.pm/elixir/library-guidelines.html#content

The module name isn't a big deal IMHO, users can just do "alias :erl_lib, as: ErlLib" if it bothers them.

Dominic

2021年9月2日(木) 8:04 Serge Aleynikov <se...@aleynikov.org>:

Serge Aleynikov

unread,
Sep 2, 2021, 12:18:17 AM9/2/21
to Dominic Morneau, Erlang Questions
For the sake of trying out some ideas, I experimented with doing this:

$ cat > t.erl
-module(t).

-export([t/1, '__info__'/1]).

'__info__'(module)           -> 'Elixir.Temp';
'__info__'(functions)        -> [];
'__info__'(macros)           -> [];
'__info__'(Key = attributes) -> get_module_info(?MODULE, Key);
'__info__'(Key = compile)    -> get_module_info(?MODULE, Key);
'__info__'(Key = md5)        -> get_module_info(
?MODULE, Key);
'__info__'(deprecated)       -> [].

t(A) -> A + 1.

$ erlc +debug_info t.erl
$ iex
iex(1)> Temp.t(1)

** (UndefinedFunctionError) function Temp.t/1 is undefined (module Temp is not available)
   Temp.t(1)

This error makes sense - Erlang expects the module name to be 't', and Elixir expects it
to be 'Elixir.Temp', and the meta information doesn't help much.
If we rename the file to Elixir.Temp.erl and the module name to 'Elixir.Temp',
then all works well.

Wouldn't it be nice if the Erlang modules allowed aliases so that we could have a single
module compiled by Erlang to be used in both languages using their native naming convention?

Michael Truog

unread,
Sep 2, 2021, 1:43:02 AM9/2/21
to Serge Aleynikov, Erlang Questions
If you wanted to convert an Elixir module to be an Erlang module that lives in an Erlang/OTP application without a runtime dependency on the Elixir runtime, you can do that with https://github.com/okeuday/reltool_util/blob/master/ex2erl .  That approach would allow you to use Elixir macros and other Elixir features without being focused on their implementation.  If you use ex2erl, it would be best to keep updating the output with future releases of Elixir, but I wouldn't expect breaking changes due to a recent Elixir release.

Best Regards,
Michael

José Valim

unread,
Sep 2, 2021, 2:25:35 AM9/2/21
to Michael Truog, Erlang Questions
Elixir developers are encouraged to call Erlang libraries and modules as is, so I wouldn’t worry about wrapping it or changing module names.

One of the barriers was standardized documentation, but recent work on Erlang/OTP made it so ExDoc can also generate docs for Erlang projects (with an Erlang theme). You can see telemetry as an example: 

The script to build such docs is on the telemetry repo. There are still improvements planned but the current version is definitely usable.


--
Reply all
Reply to author
Forward
0 new messages