zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 02e5cb1cd4203219ae753e94c7e14cd18a918b49 (tree)
parent 5c2238fc4ad1d10f0620c931d369005b53742eb7
Author: Vexu <git@vexu.eu>
Date:   Wed, 15 Jan 2020 23:05:52 +0200

add non-exhaustive enum to langref

Diffstat:
Mdoc/langref.html.in | 44++++++++++++++++++++++++++++++++++++++++++++
Mtest/compile_errors.zig | 3+--
2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -2893,6 +2893,50 @@ test "switch using enum literals" { } {#code_end#} {#header_close#} + + {#header_open|Non-exhaustive enum#} + <p> + A Non-exhaustive enum can be created by adding a trailing '_' field. + It must specify a tag type and cannot consume every enumeration value. + </p> + <p> + {#link|@intToEnum#} on a non-exhaustive enum cannot fail. + </p> + <p> + A switch on a non-exhaustive enum can include a '_' prong with the following properties: + <ul> + <li>makes it a compile error if all the known tag names are not handled by the switch</li> + <li>allows omitting {#syntax#}else{#endsyntax#}</li> + </ul> + </p> + {#code_begin|test#} +const std = @import("std"); +const assert = std.debug.assert; + +const Number = enum(u8) { + One, + Two, + Three, + _, +}; + +test "switch on non-exhaustive enum" { + const number = Number.One; + const result = switch (number) { + .One => true, + .Two, + .Three => false, + _ => false, + }; + assert(result); + const is_one = switch (number) { + .One => true, + else => false, + }; + assert(is_one); +} + {#code_end#} + {#header_close#} {#header_close#} {#header_open|union#} diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -10,9 +10,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\}; \\const B = enum(u1) { \\ a, - \\ b, \\ _, - \\ c, + \\ b, \\}; \\pub export fn entry() void { \\ _ = A;