Enum Methods and Backing Integers
Add behavior and control representation.
Enum Methods and Backing Integers is a free Zig Academy lesson on CoddyKit — lesson 4 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.
Enums Can Have Behavior
An enum is a real type, so it can hold methods. You declare functions inside the enum body, right next to the value names.
A Method on an Enum
Write a function inside the enum and take self as the first parameter. It works just like a method on a struct.
const Color = enum { red, green, fn isRed(self: Color) bool { return self == .red; } };Calling Enum Methods
You call a method with dot syntax on a value. The value becomes self, so the method knows which variant it was called on.
const ok = c.isRed();Every Enum Has Integer Backing
Under the hood, each enum value maps to an integer. Zig picks a small backing integer type that fits all your values.
Default Numbering Starts at Zero
By default the first value is 0 and each later value counts up by one. So red is 0, green is 1, and so on by order.
const Color = enum { red, green, blue }; // 0, 1, 2Choosing the Backing Type
You can name the backing integer in parentheses, like enum(u8). This pins the exact width and matters for layout and interop.
const Code = enum(u8) { a, b, c };Setting Explicit Values
Assign a specific number to a value with =. This is handy when the integers must match a protocol or hardware register.
const Status = enum(u8) { ok = 0, busy = 1, err = 255 };Enum to Integer
Turn an enum value into its number with @intFromEnum. This gives you the underlying integer to send or store.
const n = @intFromEnum(Status.err); // 255Integer to Enum
Go the other way with @enumFromInt to convert a number back into an enum value of the type you expect.
const s: Status = @enumFromInt(1);Convert with Care
@enumFromInt assumes the number is a valid variant. In safe builds a bad value is caught; otherwise it is your job to validate.
Methods Plus Tags Together
Combine both ideas: pick a backing type for layout, set values for a protocol, and add methods so the enum stays self-contained.
Quick Check
You have a Status enum value and need its underlying number. Which builtin?
Recap
You gave enums methods with self, chose a backing integer like enum(u8), set explicit values, and converted both ways with @intFromEnum and @enumFromInt. 🔢
Frequently asked questions
Is the “Enum Methods and Backing Integers” lesson free?
Yes — the full text of “Enum Methods and Backing Integers” 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 “Enum Methods and Backing Integers”?
Add behavior and control representation. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enum Methods and Backing Integers” 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