-
Notifications
You must be signed in to change notification settings - Fork 1
/
08_unions.zig
62 lines (53 loc) · 1.42 KB
/
08_unions.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! see: https://ziglang.org/documentation/master/#Zig-Test
//! see: https://ziglang.org/documentation/master/std/#builtin;is_test
// types
// may create an `extern union` to be C ABI compatible.
const Payload1 = union {
Integer: i64,
Float: f64,
Boolean: bool,
};
// must created a tagged union to use switch
const Payload2Label = enum {
Integer,
Float,
Boolean,
};
const Payload2 = union(Payload2Label) {
Integer: i64,
Float: f64,
Boolean: bool,
};
pub fn main() !void {
const file_name = std.fs.path.basename(@src().file);
print("\n file: {}\n", .{file_name});
}
// anonymous union literal syntax
fn makeBooleanPayload(b: bool) Payload1 {
return .{ .Boolean = b };
}
test "union value" {
var payload = Payload1{ .Integer = 42 };
// will trigger access of inactive union field error
// payload.Float = 42.42;
expect(payload.Integer == 42);
}
test "anonymous union literal" {
const payload = makeBooleanPayload(false);
expect(payload.Boolean == false);
}
test "tagged union switch" {
var payload = Payload2{ .Integer = 42 };
expect(payload.Integer == 42);
const str = switch (payload) {
.Integer => "an integer",
.Float => "a float",
.Boolean => "a boolean",
};
expect(eql(u8, str, "an integer"));
}
// imports
const std = @import("std");
const print = std.debug.print;
const expect = std.testing.expect;
const eql = std.mem.eql;