authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-03 23:09:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-03 23:09:58-04:00
logc2cf04086a74a3c0ac31dbc1b8d01de76988c7ae
tree8ff02486074ecf1965470d4719a503b9b15edcb7
parentd1cda00b36d687ba2c5df463ffad30c4f7881ee9
signaturelock-open Commit is signed but in an unrecognized format.

add docs for enum literals

closes #683

1 files changed, 88 insertions(+), 3 deletions(-)

doc/langref.html.in+88-3
......@@ -2350,12 +2350,12 @@ fn doTheTest() void {
23502350 var full = Full{ .number = 0x1234 };
23512351 var divided = @bitCast(Divided, full);
23522352 switch (builtin.endian) {
2353 builtin.Endian.Big => {
2353 .Big => {
23542354 assert(divided.half1 == 0x12);
23552355 assert(divided.quarter3 == 0x3);
23562356 assert(divided.quarter4 == 0x4);
23572357 },
2358 builtin.Endian.Little => {
2358 .Little => {
23592359 assert(divided.half1 == 0x34);
23602360 assert(divided.quarter3 == 0x2);
23612361 assert(divided.quarter4 == 0x1);
......@@ -2630,6 +2630,8 @@ test "@tagName" {
26302630 assert(mem.eql(u8, @tagName(Small.Three), "Three"));
26312631}
26322632 {#code_end#}
2633 {#see_also|@memberName|@memberCount|@tagName|@sizeOf#}
2634
26332635 {#header_open|extern enum#}
26342636 <p>
26352637 By default, enums are not guaranteed to be compatible with the C ABI:
......@@ -2646,6 +2648,7 @@ const Foo = extern enum { A, B, C };
26462648export fn entry(foo: Foo) void { }
26472649 {#code_end#}
26482650 {#header_close#}
2651
26492652 {#header_open|packed enum#}
26502653 <p>By default, the size of enums is not guaranteed.</p>
26512654 <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the
......@@ -2664,8 +2667,40 @@ test "packed enum" {
26642667 {#code_end#}
26652668 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
26662669 {#header_close#}
2667 {#see_also|@memberName|@memberCount|@tagName|@sizeOf#}
2670
2671 {#header_open|Enum Literals#}
2672 <p>
2673 Enum literals allow specifying the name of an enum field without specifying the enum type:
2674 </p>
2675 {#code_begin|test#}
2676const std = @import("std");
2677const assert = std.debug.assert;
2678
2679const Color = enum {
2680 Auto,
2681 Off,
2682 On,
2683};
2684
2685test "enum literals" {
2686 const color1: Color = .Auto;
2687 const color2 = Color.Auto;
2688 assert(color1 == color2);
2689}
2690
2691test "switch using enum literals" {
2692 const color = Color.On;
2693 const result = switch (color) {
2694 .Auto => false,
2695 .On => true,
2696 .Off => false,
2697 };
2698 assert(result);
2699}
2700 {#code_end#}
2701 {#header_close#}
26682702 {#header_close#}
2703
26692704 {#header_open|union#}
26702705 <p>
26712706 A bare {#syntax#}union{#endsyntax#} defines a set of possible types that a value
......@@ -3006,7 +3041,57 @@ test "switch on tagged union" {
30063041}
30073042 {#code_end#}
30083043 {#see_also|comptime|enum|@compileError|Compile Variables#}
3044
3045 {#header_open|Exhaustive Switching#}
3046 <p>
3047 When a {#syntax#}switch{#endsyntax#} expression does not have an {#syntax#}else{#endsyntax#} clause,
3048 it must exhaustively list all the possible values. Failure to do so is a compile error:
3049 </p>
3050 {#code_begin|test_err|not handled in switch#}
3051const Color = enum {
3052 Auto,
3053 Off,
3054 On,
3055};
3056
3057test "exhaustive switching" {
3058 const color = Color.Off;
3059 switch (color) {
3060 Color.Auto => {},
3061 Color.On => {},
3062 }
3063}
3064 {#code_end#}
30093065 {#header_close#}
3066
3067 {#header_open|Switching with Enum Literals#}
3068 <p>
3069 {#link|Enum Literals#} can be useful to use with {#syntax#}switch{#endsyntax#} to avoid
3070 repetitively specifying {#link|enum#} or {#link|union#} types:
3071 </p>
3072 {#code_begin|test#}
3073const std = @import("std");
3074const assert = std.debug.assert;
3075
3076const Color = enum {
3077 Auto,
3078 Off,
3079 On,
3080};
3081
3082test "enum literals with switch" {
3083 const color = Color.Off;
3084 const result = switch (color) {
3085 .Auto => false,
3086 .On => false,
3087 .Off => true,
3088 };
3089 assert(result);
3090}
3091 {#code_end#}
3092 {#header_close#}
3093 {#header_close#}
3094
30103095 {#header_open|while#}
30113096 <p>
30123097 A while loop is used to repeatedly execute an expression until