aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorzooster <r00ster91@proton.me>2022-12-26 23:54:27 +0100
committerVeikka Tuominen <git@vexu.eu>2022-12-27 12:53:41 +0200
commit547e3684bea1fce9b14ffd40be18dcc88479d5a0 (patch)
tree26d81a7b32f15684f0d11cf9c9c64ab09d5b22f1 /doc
parent3a7a39cb913387c997ed457535e53ca054d2465d (diff)
downloadzig-547e3684bea1fce9b14ffd40be18dcc88479d5a0.tar.gz
zig-547e3684bea1fce9b14ffd40be18dcc88479d5a0.zip
langref: more explicitly document how enum overriding works
Diffstat (limited to 'doc')
-rw-r--r--doc/langref.html.in19
1 files changed, 17 insertions, 2 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index 4f39ad72dd..8ba932c2bc 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -3633,9 +3633,8 @@ const Value = enum(u2) {
one,
two,
};
-
// Now you can cast between u2 and Value.
-// The ordinal value starts from 0, counting up for each member.
+// The ordinal value starts from 0, counting up by 1 from the previous member.
test "enum ordinal value" {
try expect(@enumToInt(Value.zero) == 0);
try expect(@enumToInt(Value.one) == 1);
@@ -3654,6 +3653,22 @@ test "set enum ordinal value" {
try expect(@enumToInt(Value2.million) == 1000000);
}
+// You can also override only some values.
+const Value3 = enum(u4) {
+ a,
+ b = 8,
+ c,
+ d = 4,
+ e,
+};
+test "enum implicit ordinal values and overridden values" {
+ try expect(@enumToInt(Value3.a) == 0);
+ try expect(@enumToInt(Value3.b) == 8);
+ try expect(@enumToInt(Value3.c) == 9);
+ try expect(@enumToInt(Value3.d) == 4);
+ try expect(@enumToInt(Value3.e) == 5);
+}
+
// Enums can have methods, the same as structs and unions.
// Enum methods are not special, they are only namespaced
// functions that you can call with dot syntax.