← Back to lesson 23: Describe the data contract

Describe the data 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.

Add named types and function specs, then use Dialyzer to inspect suspicious paths.

Write a result type and a spec for a positive-number check.

Run this first

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

Original full example
%% Let tools and readers see both result branches
-type result(T) :: {ok, T} | {error, not_positive}.
-spec check(integer()) -> result(integer()).
check(N) when N > 0 -> {ok, N};
check(_N) -> {error, not_positive}.

check(3) returns {ok,3} and check(0) returns {error,not_positive}.

Steps for the full example

  1. Read -type: Name a reusable term shape.
  2. Read -spec: Declare a function contract.
  3. Read ::: Separate a name or function from its type.

Original lesson notes

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

A named type gives a readable label to an Erlang term shape.

A function spec documents accepted argument types and possible return values.

Symbols and terms
-type
Name a reusable term shape.
-spec
Declare a function contract.
::
Separate a name or function from its type.
type
A named type gives a readable label to an Erlang term shape.
spec
A function spec documents accepted argument types and possible return values.
Dialyzer
Dialyzer compares inferred success types with specs to find some inconsistent paths.

Original full exercise

Write a spec for a binary length function.

Exercise starter
-spec length_of(____) -> non_neg_integer().

Accept binary().

Show hint

Use Erlang's built-in binary type.

Show original answer
Reference answer
-spec length_of(binary()) -> non_neg_integer().
Does a spec check values at runtime?

No. Specs support documentation and static analysis; runtime validation still needs code.

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

Why this shape?

Why write specs if the code can run without them?

Choice

A spec leaves a function contract for people and Dialyzer. It is good at finding calls that cannot succeed; it does not guard every runtime value.

Cost and boundary

A very broad spec says little, and an incorrect spec does not become runtime validation. Parse and validate data at system boundaries.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
The compiler performs static type checks during a build.
What BEAM changes
Dialyzer uses success typing; its goal is not to prove that every input is safe.
False friend
A clean Dialyzer run cannot prevent a bad runtime message.

Python

Familiar starting point
Type hints checked by mypy or pyright.
What BEAM changes
Specs also serve tools and readers, but the analysis model differs.
False friend
A spec is not an input-validation library.

JavaScript

Familiar starting point
TypeScript checks types before emitting JavaScript.
What BEAM changes
BEAM still runs terms; Dialyzer looks for contradictions among calls that can succeed.
False friend
Files and network boundaries still need real validation.
Original course references