Enums for a Fixed Set of Values
Name a closed list of options.
Enums for a Fixed Set of Values is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Closed List of Choices
Some values come from a small, fixed menu: a traffic light is red, yellow, or green. An enum names exactly that closed list of options.
Declaring an Enum
You write enum and list the names you want inside braces. Each name is one valid value of that type.
const Color = enum { red, green, blue };Enum Values Are Names
Each entry like red or blue is a real value of the type, not a string. You refer to it through the enum, keeping it tidy and typed.
const c: Color = Color.red;The Dot Shorthand
When Zig already knows the expected type, you can drop the name and just write a dot before the value.
const c: Color = .green;Comparing Enum Values
You compare enum values with == just like numbers. This makes branching on a fixed choice clean and readable.
if (c == .blue) {}Only Listed Values Are Allowed
An enum can hold only the names you declared. Try to use one that does not exist and the compiler rejects it right away.
const c: Color = .purple; // errorSelf-Documenting Code
Reading .green is far clearer than reading the number 1. Enums turn magic numbers into meaningful names you can trust.
Enums in Function Parameters
Accept an enum as a parameter and a caller can only pass one of your valid options, so bad input is caught at compile time.
fn paint(c: Color) void {}Switch Pairs Well with Enums
Enums shine with switch: you handle each named case, and Zig reminds you if you forget one of the options.
switch (c) { .red => {}, .green => {}, .blue => {} }Naming Conventions
Enum types use TitleCase like Color, while the individual values use lower snake_case like light_blue. This keeps the two roles distinct.
Why Reach for an Enum
Whenever a value is one of a few known states, an enum makes the choices explicit, safe, and impossible to misspell silently.
Quick Check
You want a type that is exactly one of red, green, or blue. What fits?
Recap
You met enums: a type that names a fixed set of values. Use the dot shorthand, compare with ==, and let the compiler block invalid choices. 🚦
Frequently asked questions
Is the “Enums for a Fixed Set of Values” lesson free?
Yes — the full text of “Enums for a Fixed Set of Values” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enums for a Fixed Set of Values”?
Name a closed list of options. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enums for a Fixed Set of Values” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Enums for a Fixed Set of Values
- Unions That Hold One of Many Types
- Tagged Unions and switch
- Enum Methods and Backing Integers