enum
1/8 test "enum ordinal value"...OK
2/8 test "set enum ordinal value"...OK
3/8 test "enum method"...OK
4/8 test "enum variant switch"...OK
5/8 test "@TagType"...OK
6/8 test "@memberCount"...OK
7/8 test "@memberName"...OK
8/8 test "@tagName"...OK
All tests passed.
See also:
By default, enums are not guaranteed to be compatible with the C ABI:
test.zig
const Foo = enum { A, B, C };
export fn entry(foo: Foo) void { }
test.zig
const Foo = extern enum { A, B, C };
$ zig build-obj test.zig
By default, the size of enums is not guaranteed.
packed enum
causes the size of the enum to be the same as the size of the integer tag type of the enum:
$ zig test test.zig
1/1 test "packed enum"...OK
All tests passed.
This makes the enum eligible to be in a .
Enum literals allow specifying the name of an enum field without specifying the enum type:
test.zig
const std = @import("std");
const assert = std.debug.assert;
const Color = enum {
Auto,
Off,
};
test "enum literals" {
const color1: Color = .Auto;
const color2 = Color.Auto;
assert(color1 == color2);
}
test "switch using enum literals" {
const color = Color.On;
const result = switch (color) {
.Auto => false,
.On => true,
.Off => false,
};
}