Behaviours — Erlang vs Elixir

… and protocols vs behaviours while we are at it

Attila Gulyas
Scientific breakthrough of the afternoon
1 min readOct 13, 2018

--

This is a draft, but posting so that anyone with more knowledge can comment on it.

Documented Erlang behaviours: gen_server, gen_event, gen_statem, supervisor and application. (Fun fact: supervisor is implemented using gen_server. https://github.com/erlang/otp/blob/master/lib/stdlib/src/supervisor.erl )

Erlang has many undocumented behaviours, e.g. ssh_sftpd_file_api. It seems that Erlang behaviours are centered around processes, but this doesn’t seem to stand for long: ssh_sftpd_file_api's implementation is ssh_sftpd_file, which basically an example how Erlang behaviours are used as protocols in may places (see Erlang Battlegrounds’ post).

On the other hand, Elixir uses behaviours where protocols would suffice but it would be too slow that way, e.g., Access. (This is based on Jose Valim’s comments below, but I can’t comprehend it yet.)

(10:19:50 PM) josevalim: you only consolidate inside a project and after compilation
(10:20:03 PM) josevalim: this means all the access during compilation and in scripts still won’t be fast enough, which is a no-go

Putting it another way, Erlang doesn’t have protocols, therefore it uses behaviours instead when needed. Elixir has protocols, but uses behaviours instead for performance reasons when appropriate. If I understand it correctly, behaviours could be implemented using protocols in Elixir, but it would be slow in many cases. (Again, why?)

Threads on protocol vs behaviour:

Useful links:

--

--