jonnifyelixir
F3 · The Elixir Language / F3.02 · match

F3 · Foundations · module 2

Pattern matching

In most languages = means assignment. In Elixir it is the match operator: it asserts that the value on the right has the shape on the left, and binds any names in that shape. When the shapes disagree, the match fails — and that failure is a feature, not a bug.

This is also where the course stops being abstract. From here on, the practical thread is a single project: build this learning portal in Elixir — magic-link sign-in, lesson progress, and later a Phoenix LiveView interface with Telemetry analytics. Every example in this module is a slice of that portal, starting with the data its sign-in returns.

Assignment vs. match

x = 1 READ AS ASSIGNMENT NAME x VALUE 1 1 = x rejected — cannot assign to 1 one direction: name ← value

x = 1 · assignment
A one-way store: the value lands in the name, and 1 = x is a syntax error.

= is a match

A match has two sides: a pattern on the left and a value on the right. Elixir checks that the value fits the pattern; where the pattern has an unbound name, it binds. The portal's sign-in returns a tagged tuple — {:ok, claims} on success, {:error, reason} on failure — so a match both checks which happened and extracts what came with it. Select a case to see what matches and what binds.

The match operator · select a case

PATTERN {:ok, claims} matches — binds below VALUE {:ok, %{email: "ada@portal.dev"}}

      

A match is an assertion. If you expect {:ok, _} and get {:error, _}, the program stops there rather than carrying a wrong assumption forward — the heart of “let it crash.”

The project: a learning portal

The portal is the one you are reading, rebuilt in Elixir. A learner signs in with a magic link — no password — issued by an external auth provider for the current course; the portal verifies the link, starts a session, and tracks which lessons that learner has completed. Each entity carries a branded Snowflake id, the same cross-system pivot key used throughout the course.

Portal.Accounts
learners — %User{id: "USR0NbAb1xcFCy", email: "ada@portal.dev"}.
Portal.Auth
magic-link sign-in: request_link/1 and verify/1, which returns {:ok, claims} or {:error, :expired | :invalid}; sessions like %Session{id: "SES0NbAb29FnXc"}.
Portal.Catalog
courses and lessons — %Lesson{id: "LSN0NbAb2Lk9GS"}.
Portal.Progress
completions and the events that drive them — {:lesson_completed, user_id, lesson_id}.

The external provider hands the portal a signed token; decoding it yields claims — a map of email, course, and an expiry. Reading those claims safely is a pattern-matching problem, which is why the project starts here.

Three deep dives

This module continues across three pages, each a facet of pattern matching grounded in the portal. Read them in order.

F3.02.1
The match operator
= asserts and binds; the pin ^ matches a value you already hold. Seen through verifying a sign-in.
F3.02.2
Destructuring portal data
Pull fields out of tuples, lists, maps, and structs — claims, request params, and progress records — in one match.
F3.02.3
Branching with case, with & guards
Dispatch on shape: function heads, case, guards, and the with pipeline that runs the whole sign-in.

F1 · Algebra

An equation asserts that both sides denote the same value.

F3 · the match operator

= asserts the value matches the pattern, and binds the names in it.

Start with the match operator, then destructuring, then branching. Next module: F3.03 — Functions, modules & the pipe.

References

Primary sources for this lesson, and where it connects in the course.