-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_structs.zig
76 lines (65 loc) · 1.94 KB
/
06_structs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! see: https://ziglang.org/documentation/master/#Anonymous-List-Literals
//! see: https://ziglang.org/documentation/master/#struct
// types
const Starship1 = struct {
name: []const u8,
number: u32,
};
// structs with functions behave like methods (namespaced functions).
const Starship2 = struct {
name: []const u8,
number: u32,
pub fn isSimulation(self: Starship2) bool {
return eql(u8, self.name, "Kobayashi Maru");
}
};
// extern struct should only be used for C ABI compatibility.
// see: https://ziglang.org/documentation/master/#extern-struct
const Starship3 = extern struct {
name: []const u8,
number: u32,
};
pub fn main() !void {
const file_name = std.fs.path.basename(@src().file);
print("\n file: {}\n", .{file_name});
}
// anonymous list literals look like structs without field names.
// anonymous list literals are passed as arguments for `print` which
// accept an `anytype` and perform comptime/runtime logic.
test "anonymous list literal" {
const list = .{ 1, 2, 3 };
print(" @TypeOf(list) is {}\n", .{@typeName(@TypeOf(list))});
expect(list[2] == 3);
}
test "anonymous struct" {
const stct = .{ .a = "one", .b = 1 };
print(" @TypeOf(stct) is {}\n", .{@typeName(@TypeOf(stct))});
expect(eql(u8, stct.a, "one"));
}
test "struct Starship" {
const ship = Starship1{
.name = "Enterprise",
.number = 1701,
};
print(" ship: {}\n", .{ship});
}
test "coerced struct Starship" {
const ship: Starship1 = .{
.name = "Yamato",
.number = 24383,
};
print(" ship: {}\n", .{ship});
}
test "struct with methods (namespaced functions)" {
const ship = Starship2{
.name = "Kobayashi Maru",
.number = 1022,
};
print(" ship: {}\n", .{ship});
expect(ship.isSimulation() == true);
}
// imports
const std = @import("std");
const print = std.debug.print;
const expect = std.testing.expect;
const eql = std.mem.eql;