-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum.zig
91 lines (80 loc) · 2.04 KB
/
enum.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
// Declare an enum.
const Type = enum {
Ok,
NotOk,
};
// Enums are sum types, and can hold more complex data of different types.
const ComplexType = enum {
Ok: u8,
NotOk: void,
};
// Declare a specific instance of the enum variant.
const c = ComplexType.Ok { 0 };
// The ordinal value of a simple enum with no data members can be
// retrieved by a simple cast.
// The value starts from 0, counting up for each member.
const Value = enum {
Zero,
One,
Two,
};
test "enum ordinal value" {
assert(usize(Value.Zero) == 0);
assert(usize(Value.One) == 1);
assert(usize(Value.Two) == 2);
}
// Enums can have methods, the same as structs.
// Enum methods are not special, they are only namespaced
// functions that you can call with dot syntax.
const Suit = enum {
Clubs,
Spades,
Diamonds,
Hearts,
pub fn ordinal(self: &const Suit) -> u8 {
u8(*self)
}
};
test "enum method" {
const p = Suit.Spades;
assert(p.ordinal() == 1);
}
// An enum variant of different types can be switched upon.
// The associated data can be retrieved using `|...|` syntax.
//
// A void type is not required on a tag-only member.
const Foo = enum {
String: []const u8,
Number: u64,
None,
};
test "enum variant switch" {
const p = Foo.Number { 54 };
const what_is_it = switch (p) {
// Capture by reference
Foo.String => |*x| {
"this is a string"
},
// Capture by value
Foo.Number => |x| {
"this is a number"
},
Foo.None => {
"this is a none"
}
};
}
// The @enumTagName and @memberCount builtin functions can be used to
// the string representation and number of members respectively.
const BuiltinType = enum {
A: f32,
B: u32,
C,
};
test "enum builtins" {
assert(mem.eql(u8, @enumTagName(BuiltinType.A { 0 }), "A"));
assert(mem.eql(u8, @enumTagName(BuiltinType.C), "C"));
assert(@memberCount(BuiltinType) == 3);
}