← Back to lesson 24: Write down the data contract

Write down 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.

Describe public input and output shapes with `@type` and `@spec`.

Add a result type and a function spec, then run the function with valid and invalid input.

Run this first

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

Original full example
# Describe both result branches before running them
defmodule Positive do
  @type result(a) :: {:ok, a} | {:error, :not_positive}
  @spec check(integer()) :: result(integer())
  def check(number) when number > 0, do: {:ok, number}
  def check(_number), do: {:error, :not_positive}
end

{Positive.check(3), Positive.check(0)}

The result is {{:ok, 3}, {:error, :not_positive}}.

Steps for the full example

  1. Read @type result(a): Name a reusable tagged-result shape.
  2. Read @spec: Declare a function's input and output contract.
  3. Read ::: Separate a name or expression from its type.

Original lesson notes

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

@type gives a readable name to a term shape.

@spec documents accepted arguments and possible return values.

Symbols and terms
@type result(a)
Name a reusable tagged-result shape.
@spec
Declare a function's input and output contract.
::
Separate a name or expression from its type.
type alias
@type gives a readable name to a term shape.
function spec
@spec documents accepted arguments and possible return values.
Dialyzer
Dialyzer compares inferred success types with specs to find suspicious paths.

Original full exercise

Write a spec for a function that accepts a string and returns its length.

Exercise starter
@spec length_of(____) :: ____
def length_of(text), do: String.length(text)

Use String.t() and non_neg_integer().

Show hint

A length cannot be negative.

Show original answer
Reference answer
@spec length_of(String.t()) :: non_neg_integer()
def length_of(text), do: String.length(text)
Does a typespec validate data at runtime?

No. Specs document and support static analysis; runtime checks still need 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