← Back to lesson 23: Write a callback contract

Write a callback 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.

Define a generic behaviour and make a module satisfy it.

Declare Greeter.greet/1, implement it, and call the implementation directly.

Run this first

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

Original full example
# Define a contract without creating a process
defmodule Greeter do
  @callback greet(String.t()) :: String.t()
end
defmodule Friendly do
  @behaviour Greeter
  @impl true
  def greet(name), do: "Hello, #{name}"
end

Friendly.greet("Maya")

The call returns "Hello, Maya". No process or GenServer is involved.

Steps for the full example

  1. Read @callback: Declare one required function signature.
  2. Read @behaviour: State which contract a module implements.
  3. Read term(): Accept or return any Erlang term in a typespec.

Original lesson notes

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

A behaviour declares callbacks that implementing modules promise to provide.

A callback gives a function name, arguments, and return type expected by the contract.

Symbols and terms
@callback
Declare one required function signature.
@behaviour
State which contract a module implements.
term()
Accept or return any Erlang term in a typespec.
behaviour
A behaviour declares callbacks that implementing modules promise to provide.
callback
A callback gives a function name, arguments, and return type expected by the contract.
implementation module
@behaviour asks the compiler to check that required callbacks exist.

Original full exercise

Add a Formal module that implements the same callback.

Exercise starter
defmodule Formal do
  @behaviour ____
  @impl true
  def greet(name), do: "Good day, #{____}"
end

Formal.greet("Maya") should return "Good day, Maya".

Show hint

Use Greeter and name.

Show original answer
Reference answer
defmodule Formal do
  @behaviour Greeter
  @impl true
  def greet(name), do: "Good day, #{name}"
end
Does every behaviour start a process?

No. A behaviour is a callback contract; OTP behaviours add process machinery later.

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