mirror of
https://github.com/ziglang/zig.git
synced 2025-12-18 12:13:20 +00:00
langref: more explicitly document how enum overriding works
This commit is contained in:
parent
3a7a39cb91
commit
547e3684be
@ -3633,9 +3633,8 @@ const Value = enum(u2) {
|
|||||||
one,
|
one,
|
||||||
two,
|
two,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Now you can cast between u2 and Value.
|
// 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" {
|
test "enum ordinal value" {
|
||||||
try expect(@enumToInt(Value.zero) == 0);
|
try expect(@enumToInt(Value.zero) == 0);
|
||||||
try expect(@enumToInt(Value.one) == 1);
|
try expect(@enumToInt(Value.one) == 1);
|
||||||
@ -3654,6 +3653,22 @@ test "set enum ordinal value" {
|
|||||||
try expect(@enumToInt(Value2.million) == 1000000);
|
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.
|
// Enums can have methods, the same as structs and unions.
|
||||||
// Enum methods are not special, they are only namespaced
|
// Enum methods are not special, they are only namespaced
|
||||||
// functions that you can call with dot syntax.
|
// functions that you can call with dot syntax.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user