← Back to lesson 22: Define a module contract

Define a module contract: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Declare a callback and implement it in a separate module.

Create a greeter behaviour and one module that satisfies its callback.

Run this first

Run it once, change one input, and compare the new result.

Original full example
%% greeter.erl declares the contract
-module(greeter).
-callback greet(binary()) -> binary().

%% friendly.erl implements it
-module(friendly).
-behaviour(greeter).
-export([greet/1]).
greet(Name) -> <<"Hello, ", Name/binary>>.

After compiling both modules, friendly:greet(<<"Maya">>) returns <<"Hello, Maya">>.

Steps for the full example

  1. Read -callback: Declare one required function signature.
  2. Read -behaviour(Module).: State which contract this module implements.
  3. Read -export([greet/1]).: Make the callback callable.

Original lesson notes

Run the example first. Predict one result, then change one input and run it again.

A behaviour lists callback functions that implementing modules promise to export.

A callback specification names the arguments and return type required by the contract.

Symbols and terms
-callback
Declare one required function signature.
-behaviour(Module).
State which contract this module implements.
-export([greet/1]).
Make the callback callable.
behaviour
A behaviour lists callback functions that implementing modules promise to export.
callback
A callback specification names the arguments and return type required by the contract.
implementation
The behaviour attribute asks the compiler to warn when callbacks are missing.

Original full exercise

Add the behaviour attribute to another module.

Exercise starter
-module(formal).
____
-export([greet/1]).

Declare greeter as the behaviour.

Show hint

Use the -behaviour(...) attribute.

Show original answer
Reference answer
-module(formal).
-behaviour(greeter).
-export([greet/1]).
Does a behaviour start a process?

No. It is a module contract. OTP behaviours add process patterns in later lessons.

Original lesson takeaways
Design trade-offs and language comparisons
DESIGN DECISION

Why this shape?

Why does a behaviour define callbacks without creating a process?

Choice

A behaviour first states which functions a module must implement. GenServer/gen_server is a behaviour with a runtime protocol, but a general behaviour starts nothing by itself.

Cost and boundary

It standardizes a boundary. It does not choose state ownership, supervision, capacity, or error policy for you.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
An interface defines a method contract.
What BEAM changes
A behaviour constrains module/function/arity entries, which callers still invoke as module functions.
False friend
Implementing one creates neither an object nor a thread.

Python

Familiar starting point
An ABC or Protocol.
What BEAM changes
Callback metadata helps tools check an implementation and lets OTP invoke it.
False friend
It is not a decorator that wraps runtime calls.

JavaScript

Familiar starting point
A conventional object shape or TypeScript interface.
What BEAM changes
A behaviour lives at the BEAM module and callback-entry level.
False friend
A type contract and a process lifecycle remain separate decisions.
Original course references