authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-12-26 23:54:27+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-27 12:53:41+02:00
log547e3684bea1fce9b14ffd40be18dcc88479d5a0
tree26d81a7b32f15684f0d11cf9c9c64ab09d5b22f1
parent3a7a39cb913387c997ed457535e53ca054d2465d

langref: more explicitly document how enum overriding works


1 files changed, 17 insertions(+), 2 deletions(-)

doc/langref.html.in+17-2
...@@ -3633,9 +3633,8 @@ const Value = enum(u2) {...@@ -3633,9 +3633,8 @@ const Value = enum(u2) {
3633 one,3633 one,
3634 two,3634 two,
3635};3635};
3636
3637// Now you can cast between u2 and Value.3636// Now you can cast between u2 and Value.
3638// The ordinal value starts from 0, counting up for each member.3637// The ordinal value starts from 0, counting up by 1 from the previous member.
3639test "enum ordinal value" {3638test "enum ordinal value" {
3640 try expect(@enumToInt(Value.zero) == 0);3639 try expect(@enumToInt(Value.zero) == 0);
3641 try expect(@enumToInt(Value.one) == 1);3640 try expect(@enumToInt(Value.one) == 1);
...@@ -3654,6 +3653,22 @@ test "set enum ordinal value" {...@@ -3654,6 +3653,22 @@ test "set enum ordinal value" {
3654 try expect(@enumToInt(Value2.million) == 1000000);3653 try expect(@enumToInt(Value2.million) == 1000000);
3655}3654}
36563655
3656// You can also override only some values.
3657const Value3 = enum(u4) {
3658 a,
3659 b = 8,
3660 c,
3661 d = 4,
3662 e,
3663};
3664test "enum implicit ordinal values and overridden values" {
3665 try expect(@enumToInt(Value3.a) == 0);
3666 try expect(@enumToInt(Value3.b) == 8);
3667 try expect(@enumToInt(Value3.c) == 9);
3668 try expect(@enumToInt(Value3.d) == 4);
3669 try expect(@enumToInt(Value3.e) == 5);
3670}
3671
3657// Enums can have methods, the same as structs and unions.3672// Enums can have methods, the same as structs and unions.
3658// Enum methods are not special, they are only namespaced3673// Enum methods are not special, they are only namespaced
3659// functions that you can call with dot syntax.3674// functions that you can call with dot syntax.