union

    test.zig

    Shell

    1. $ zig test test.zig
    2. 1/1 test "simple union"... thread 793111 panic: access of inactive union field
    3. /home/andy/Downloads/zig/docgen_tmp/test.zig:8:12: 0x207a4b in test "simple union" (test)
    4. payload.float = 12.34;
    5. ^
    6. /home/andy/Downloads/zig/lib/std/special/test_runner.zig:80:28: 0x22f453 in std.special.main (test)
    7. } else test_fn.func();
    8. ^
    9. /home/andy/Downloads/zig/lib/std/start.zig:543:22: 0x227e4c in std.start.callMain (test)
    10. root.main();
    11. ^
    12. /home/andy/Downloads/zig/lib/std/start.zig:495:12: 0x20926e in std.start.callMainWithArgs (test)
    13. return @call(.{ .modifier = .always_inline }, callMain, .{});
    14. ^
    15. /home/andy/Downloads/zig/lib/std/start.zig:409:17: 0x208306 in std.start.posixCallMainAndExit (test)
    16. std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
    17. ^
    18. /home/andy/Downloads/zig/lib/std/start.zig:322:5: 0x208112 in std.start._start (test)
    19. @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    20. ^
    21. error: the following test command crashed:
    22. docgen_tmp/zig-cache/o/8e4964e05ddaf866e77360a551b3c05b/test /home/andy/Downloads/zig/build-release/zig

    You can activate another field by assigning the entire union:

    test_simple_union.zig

    1. const std = @import("std");
    2. const expect = std.testing.expect;
    3. const Payload = union {
    4. int: i64,
    5. boolean: bool,
    6. };
    7. var payload = Payload{ .int = 1234 };
    8. try expect(payload.int == 1234);
    9. payload = Payload{ .float = 12.34 };
    10. try expect(payload.float == 12.34);
    11. }

    Shell

    1. $ zig test test_simple_union.zig
    2. 1/1 test "simple union"... OK
    3. All 1 tests passed.

    In order to use with a union, it must be a Tagged union.

    To initialize a union when the tag is a -known name, see @unionInit.

    test_switch_tagged_union.zig

    Shell

    1. $ zig test test_switch_tagged_union.zig
    2. 1/3 test "switch on tagged union"... OK
    3. 2/3 test "get tag type"... OK
    4. 3/3 test "coerce to enum"... OK
    5. All 3 tests passed.

    In order to modify the payload of a tagged union in a switch expression, place a * before the variable name to make it a pointer:

    test_switch_modify_tagged_union.zig

    1. const std = @import("std");
    2. const expect = std.testing.expect;
    3. const ComplexTypeTag = enum {
    4. ok,
    5. not_ok,
    6. };
    7. const ComplexType = union(ComplexTypeTag) {
    8. ok: u8,
    9. not_ok: void,
    10. };
    11. test "modify tagged union in switch" {
    12. var c = ComplexType{ .ok = 42 };
    13. try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
    14. ComplexTypeTag.ok => |*value| value.* += 1,
    15. ComplexTypeTag.not_ok => unreachable,
    16. }
    17. try expect(c.ok == 43);
    18. }

    Shell

    1. $ zig test test_switch_modify_tagged_union.zig
    2. 1/1 test "modify tagged union in switch"... OK
    3. All 1 tests passed.

    Unions can be made to infer the enum tag type. Further, unions can have methods just like structs and enums.

    test_union_method.zig

    1. $ zig test test_union_method.zig
    2. 1/1 test "union method"... OK
    3. All 1 tests passed.

    can be used to return a comptime [:0]const u8 value representing the field name:

    test_tagName.zig

    1. const std = @import("std");
    2. const expect = std.testing.expect;
    3. const Small2 = union(enum) {
    4. a: i32,
    5. b: bool,
    6. c: u8,
    7. };
    8. test "@tagName" {
    9. try expect(std.mem.eql(u8, @tagName(Small2.a), "a"));
    10. }

    Shell

    1. $ zig test test_tagName.zig
    2. 1/1 test "@tagName"... OK
    3. All 1 tests passed.

    An extern union has memory layout guaranteed to be compatible with the target C ABI.

    See also:

    A packed union has well-defined in-memory layout and is eligible to be in a packed struct.

    syntax can be used to initialize unions without specifying the type:

    Shell

    1. $ zig test anon_union.zig
    2. All 1 tests passed.