1const std = @import("std");
2const expectEqual = std.testing.expectEqual;
3
4const ComplexTypeTag = enum {
5 ok,
6 not_ok,
7};
8const ComplexType = union(ComplexTypeTag) {
9 ok: u8,
10 not_ok: void,
11};
12
13test "switch on tagged union" {
14 const c = ComplexType{ .ok = 42 };
15 try expectEqual(ComplexTypeTag.ok, @as(ComplexTypeTag, c));
16
17 switch (c) {
18 .ok => |value| try expectEqual(42, value),
19 .not_ok => unreachable,
20 }
21
22 switch (c) {
23 .ok => |_, tag| {
24 // Because we're in the '.ok' prong, 'tag' is compile-time known to be '.ok':
25 comptime std.debug.assert(tag == .ok);
26 },
27 .not_ok => unreachable,
28 }
29}
30
31test "get tag type" {
32 try expectEqual(ComplexTypeTag, std.meta.Tag(ComplexType));
33}
34
35// test