1. 1/7 test "enum ordinal value"... OK
    2. 2/7 test "set enum ordinal value"... OK
    3. 3/7 test "enum method"... OK
    4. 4/7 test "enum variant switch"... OK
    5. 5/7 test "@TagType"... OK
    6. 6/7 test "@typeInfo"... OK
    7. 7/7 test "@tagName"... OK
    8. All 7 tests passed.

    See also:

    By default, enums are not guaranteed to be compatible with the C ABI:

    test.zig

    1. const Foo = enum { a, b, c };
    2. export fn entry(foo: Foo) void { }
    1. $ zig build-obj test.zig
    2. ./docgen_tmp/test.zig:2:22: error: parameter of type 'Foo' not allowed in function with calling convention 'C'
    3. export fn entry(foo: Foo) void {
    4. ^

    For a C-ABI-compatible enum, use extern enum:

      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:

      test.zig

      1. const std = @import("std");
      2. test "packed enum" {
      3. one,
      4. two,
      5. three,
      6. };
      7. std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
      8. }
      1. $ zig test test.zig
      2. 1/1 test "packed enum"... OK
      3. All 1 tests passed.

      This makes the enum eligible to be in a .

      test.zig

      1. $ zig test test.zig
      2. 1/2 test "enum literals"... OK
      3. 2/2 test "switch using enum literals"... OK
      4. All 2 tests passed.

      A Non-exhaustive enum can be created by adding a trailing ‘_‘ field. It must specify a tag type and cannot consume every enumeration value.

      @intToEnum on a non-exhaustive enum cannot fail.

      A switch on a non-exhaustive enum can include a ‘_‘ prong as an alternative to an else prong with the difference being that it makes it a compile error if all the known tag names are not handled by the switch.

      1. const std = @import("std");
      2. const expect = std.testing.expect;
      3. const Number = enum(u8) {
      4. one,
      5. _,
      6. };
      7. test "switch on non-exhaustive enum" {
      8. const number = Number.one;
      9. const result = switch (number) {
      10. .one => true,
      11. .two,
      12. .three => false,
      13. _ => false,
      14. };
      15. expect(result);
      16. const is_one = switch (number) {
      17. .one => true,
      18. else => false,
      19. };
      20. expect(is_one);
      21. }
      1. $ zig test test.zig
      2. 1/1 test "switch on non-exhaustive enum"... OK