User Avatar

Kamaro Lambert

2y ago

I write about principles and life lessons that make you prosper in the Spirit, Soul and Body.

Part 1: Demystifying Elixir/Erlang Behaviours: Contracts & Interfaces in OTP
By Kamaro

Have you ever struggled to grasp Elixir/Erlang behaviours?

This powerful building block is vital in the Elixir/Erlang ecosystem, enabling rich Open Telecom Protocol (OTP) features. It took me weeks to grasp what it is, but fear not; understanding behaviour doesn't have to take you weeks.

Behaviours serve as contracts between different modules, acting as a memorandum of understanding. Like interfaces in languages like PHP, Java, and C++, behaviours define expected functions that implementing modules must fulfil.

One such popular behaviour is GenServer, which comes out of the box in OTP. It requires the implementation module to have at least init() and handle_call() functions.

In summary, Elixir/Erlang behaviours are essential contracts that manage expectations between modules, akin to interfaces in other programming languages.

In the next parts, we'll see this in examples, with actual codes. Stay tuned.

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers