authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-15 23:05:52+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-15 23:05:52+02:00
log02e5cb1cd4203219ae753e94c7e14cd18a918b49
treec35cfe8bf4857550e8f58f0f1fa5354afad22b21
parent5c2238fc4ad1d10f0620c931d369005b53742eb7
signaturelock-open Commit is signed but in an unrecognized format.

add non-exhaustive enum to langref


2 files changed, 45 insertions(+), 2 deletions(-)

doc/langref.html.in+44
......@@ -2893,6 +2893,50 @@ test "switch using enum literals" {
28932893}
28942894 {#code_end#}
28952895 {#header_close#}
2896
2897 {#header_open|Non-exhaustive enum#}
2898 <p>
2899 A Non-exhaustive enum can be created by adding a trailing '_' field.
2900 It must specify a tag type and cannot consume every enumeration value.
2901 </p>
2902 <p>
2903 {#link|@intToEnum#} on a non-exhaustive enum cannot fail.
2904 </p>
2905 <p>
2906 A switch on a non-exhaustive enum can include a '_' prong with the following properties:
2907 <ul>
2908 <li>makes it a compile error if all the known tag names are not handled by the switch</li>
2909 <li>allows omitting {#syntax#}else{#endsyntax#}</li>
2910 </ul>
2911 </p>
2912 {#code_begin|test#}
2913const std = @import("std");
2914const assert = std.debug.assert;
2915
2916const Number = enum(u8) {
2917 One,
2918 Two,
2919 Three,
2920 _,
2921};
2922
2923test "switch on non-exhaustive enum" {
2924 const number = Number.One;
2925 const result = switch (number) {
2926 .One => true,
2927 .Two,
2928 .Three => false,
2929 _ => false,
2930 };
2931 assert(result);
2932 const is_one = switch (number) {
2933 .One => true,
2934 else => false,
2935 };
2936 assert(is_one);
2937}
2938 {#code_end#}
2939 {#header_close#}
28962940 {#header_close#}
28972941
28982942 {#header_open|union#}
test/compile_errors.zig+1-2
......@@ -10,9 +10,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1010 \\};
1111 \\const B = enum(u1) {
1212 \\ a,
13 \\ b,
1413 \\ _,
15 \\ c,
14 \\ b,
1615 \\};
1716 \\pub export fn entry() void {
1817 \\ _ = A;