Elixir basics · Lesson 16 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · FOUNDATION · LESSON 1625 minutes

Give domain data a shape

Define a struct with module documentation, required keys, and a function that matches it.

01 · Start with the whole example

Run this first

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

Elixir
# Define documented data and one operation
defmodule Book do
  @moduledoc "A small book record."
  @enforce_keys [:title]
  defstruct [:title, pages: 0]
  def long?(%__MODULE__{pages: pages}), do: pages >= 300
end

book = struct!(Book, title: "River", pages: 320)
{book.title, Book.long?(book)}
Check the result first
  • The result is {"River", true}.
02 · Take the code apart

Read from the first line down

  1. Read defstruct: Declare struct fields and defaults.
  2. Read @enforce_keys: Require selected fields when a struct is built.
  3. Read %__MODULE__{}: Refer to the current module's struct inside its functions.
03 · Meet the new symbols

Symbols are not secret signs

defstruct

Declare struct fields and defaults.

@enforce_keys

Require selected fields when a struct is built.

%__MODULE__{}

Refer to the current module's struct inside its functions.

04 · Ideas inside the code

Match each name to its meaning

01

struct

A struct is a map with a fixed module name and declared fields.

02

module attribute

Attributes such as @moduledoc, @doc, and @enforce_keys add compile-time information.

03

struct pattern

%Book{pages: pages} matches only a Book struct and extracts one field.

05 · Make the idea clear

Why these forms are useful

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

A struct is a map with a fixed module name and declared fields.

Attributes such as @moduledoc, @doc, and @enforce_keys add compile-time information.

06 · Change it yourself

Close the answer and try

Add an author field and build a book with that field.

Practice starting point
defmodule NoteBook do
  defstruct [:title, ____]
end

struct!(NoteBook, title: "North", ____: "Maya")

Target result: Return a NoteBook struct whose author is "Maya".

Stuck? Read one hint

Use the field name author in both blanks.

After you run it, see one answer
One answer
defmodule NoteBook do
  defstruct [:title, :author]
end

struct!(NoteBook, title: "North", author: "Maya")
Think about it: Is every map a Book struct?

No. A struct carries its module name and declared fields.

Take with you

Remember these three lines

  1. 1

    Structs give maps a domain shape.

  2. 2

    Attributes document and constrain modules.

  3. 3

    Pattern-match only the fields a function needs.

Lesson completeRun the code once, then mark this lesson as done.