| ... | @@ -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. |
| 3639 | test "enum ordinal value" { | 3638 | test "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 | } |
| 3656 | | 3655 | |
| | 3656 | // You can also override only some values. |
| | 3657 | const Value3 = enum(u4) { |
| | 3658 | a, |
| | 3659 | b = 8, |
| | 3660 | c, |
| | 3661 | d = 4, |
| | 3662 | e, |
| | 3663 | }; |
| | 3664 | test "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 namespaced | 3673 | // 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. |