Structs
- Structs. A struct is a named map with declared keys: %Order{}: typos caught at compile time.
- Defaults and enforcement. Structs declare defaults and enforced keys: objects born valid or not at all.
- Updating structs. Update syntax rebuilds: %{order | status: :paid}: new value, old one unchanged.
Protocols
- Protocols. A protocol dispatches by data type: String.Chars, Enumerable: polymorphism for data.
- Implementations. defimpl implements a protocol per struct: your types joining the ecosystem’s verbs.
- Protocols versus behaviours. Protocols dispatch on data; behaviours contract modules: two polymorphisms, two jobs.
Behaviours
- Behaviours. A behaviour declares callbacks a module must export: the compile-checked module contract.
- Callbacks. @callback specs document each required function: types included.
- Swappable modules. Behaviours make modules swappable: the real adapter in prod, a fake in tests, one contract.
Before and after this level
- Before: Level 3: Functional Design: Pipelines, Higher-Order Functions, Recursion and Transformation.
- Next: Level 5: Processes: Processes, Messages, Links and Monitors.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Elixir Level 4: Data Modelling cover?
Structs, Protocols, Behaviours: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 3: Functional Design, which covers Pipelines, Higher-Order Functions, Recursion and Transformation.