test_tagged_union.zig (528B) - Raw
1 const std = @import("std"); 2 const expect = std.testing.expect; 3 4 const ComplexTypeTag = enum { 5 ok, 6 not_ok, 7 }; 8 const ComplexType = union(ComplexTypeTag) { 9 ok: u8, 10 not_ok: void, 11 }; 12 13 test "switch on tagged union" { 14 const c = ComplexType{ .ok = 42 }; 15 try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); 16 17 switch (c) { 18 .ok => |value| try expect(value == 42), 19 .not_ok => unreachable, 20 } 21 } 22 23 test "get tag type" { 24 try expect(std.meta.Tag(ComplexType) == ComplexTypeTag); 25 } 26 27 // test