Modules and data

A module holds the functions of one idea, its attributes hold the constants and the documentation, a struct gives a map a name and a shape, a protocol lets a new type join an old function, and a behaviour is a contract a module promises to keep.

These four ideas are how an Elixir program is organised. The chapter also covers the access syntax of nested data and the protocols the standard library itself uses, so the next chapter on collections has a vocabulary to build on.

4.1 Modules in Elixir defmodule, the functions it holds, public and private, and the .beam it compiles to Open 4.2 import, alias, require, use in Elixir the four directives, what each one changes, and the import that surprises a reader Open 4.3 Structs in Elixir the struct with its defaults, enforce_keys, the update syntax, and the field that does not exist Open 4.4 Protocols in Elixir defprotocol, one implementation per type, and the dispatch that picks one at run time Open 4.5 Behaviours in Elixir the callback a module must implement, impl, and the warning the compiler gives Open 4.6 Module attributes a constant that is read at compile time, doc and moduledoc, and a value that is not there at run time Open 4.7 Nested access a struct in a struct, put_in and update_in, and the error a missing key raises Open 4.8 The protocols of the standard library Enumerable, Collectable and Inspect behind Enum, into: and IO.inspect Open

Lessons